Skip to content

Commit f4c69f6

Browse files
auto close RequestBody after request
1 parent 0989efd commit f4c69f6

File tree

5 files changed

+34
-26
lines changed

5 files changed

+34
-26
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
target/
2-
.idea/
2+
.idea/
3+
crawl/

WebCollector.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6">
44
<output url="file://$MODULE_DIR$/target/classes" />
55
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<option name="createStubs" value="true" />
67
<content url="file://$MODULE_DIR$">
78
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
89
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
<phase>package</phase>
193193
<configuration>
194194
<tasks>
195-
<delete dir="./crawl" includeemptydirs="true"/>
195+
<!--<delete dir="./crawl" includeemptydirs="true"/>-->
196196
<copy todir="${project.build.directory}/bin">
197197
<fileset dir="${project.build.directory}">
198198
<include name="*.jar"/>

src/main/java/cn/edu/hfut/dmic/webcollector/plugin/net/OkHttpRequester.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,37 +98,43 @@ public Page getResponse(CrawlDatum datum) throws Exception {
9898
String charset = null;
9999

100100
ResponseBody responseBody = response.body();
101-
int code = response.code();
102-
//设置重定向地址
103-
datum.code(code);
104-
datum.location(response.header("Location"));
101+
try {
102+
int code = response.code();
103+
//设置重定向地址
104+
datum.code(code);
105+
datum.location(response.header("Location"));
105106

106-
if(!successCodeSet.contains(code)){
107+
if (!successCodeSet.contains(code)) {
107108
// throw new IOException(String.format("Server returned HTTP response code: %d for URL: %s (CrawlDatum: %s)", code,crawlDatum.url(), crawlDatum.key()));
108109
// throw new IOException(String.format("Server returned HTTP response code: %d for %s", code, crawlDatum.briefInfo()));
109-
throw new IOException(String.format("Server returned HTTP response code: %d", code));
110+
throw new IOException(String.format("Server returned HTTP response code: %d", code));
110111

111-
}
112-
if(responseBody != null){
113-
content = responseBody.bytes();
114-
MediaType mediaType = responseBody.contentType();
115-
if(mediaType!=null){
116-
contentType = mediaType.toString();
117-
Charset responseCharset = mediaType.charset();
118-
if(responseCharset!=null){
119-
charset = responseCharset.name();
112+
}
113+
if (responseBody != null) {
114+
content = responseBody.bytes();
115+
MediaType mediaType = responseBody.contentType();
116+
if (mediaType != null) {
117+
contentType = mediaType.toString();
118+
Charset responseCharset = mediaType.charset();
119+
if (responseCharset != null) {
120+
charset = responseCharset.name();
121+
}
120122
}
121123
}
122-
}
123124

124-
Page page = new Page(
125-
datum,
126-
contentType,
127-
content
128-
);
129-
page.charset(charset);
130-
page.obj(response);
131-
return page;
125+
Page page = new Page(
126+
datum,
127+
contentType,
128+
content
129+
);
130+
page.charset(charset);
131+
page.obj(response);
132+
return page;
133+
}finally {
134+
if(responseBody != null){
135+
responseBody.close();
136+
}
137+
}
132138
}
133139

134140
public HashSet<Integer> getSuccessCodeSet() {

webcollector-2.73-alpha-bin.zip

160 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)