Skip to content

Commit c287de7

Browse files
committed
Define the constants instead of duplicating a litteral 3 times.
1 parent c942d7c commit c287de7

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

webmagic-core/src/main/java/us/codecraft/webmagic/model/HttpRequestBody.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
public class HttpRequestBody implements Serializable {
1818

1919
private static final long serialVersionUID = 5659170945717023595L;
20+
21+
private static final String ENCODING_ERROR ="illegal encoding ";
2022

2123
public static abstract class ContentType {
2224

@@ -68,15 +70,15 @@ public static HttpRequestBody json(String json, String encoding) {
6870
try {
6971
return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding);
7072
} catch (UnsupportedEncodingException e) {
71-
throw new IllegalArgumentException("illegal encoding " + encoding, e);
73+
throw new IllegalArgumentException(ENCODING_ERROR+ encoding, e);
7274
}
7375
}
7476

7577
public static HttpRequestBody xml(String xml, String encoding) {
7678
try {
7779
return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding);
7880
} catch (UnsupportedEncodingException e) {
79-
throw new IllegalArgumentException("illegal encoding " + encoding, e);
81+
throw new IllegalArgumentException(ENCODING_ERROR + encoding, e);
8082
}
8183
}
8284

@@ -92,7 +94,7 @@ public static HttpRequestBody form(Map<String,Object> params, String encoding){
9294
try {
9395
return new HttpRequestBody(URLEncodedUtils.format(nameValuePairs, encoding).getBytes(encoding), ContentType.FORM, encoding);
9496
} catch (UnsupportedEncodingException e) {
95-
throw new IllegalArgumentException("illegal encoding " + encoding, e);
97+
throw new IllegalArgumentException(ENCODING_ERROR + encoding, e);
9698
}
9799
}
98100

webmagic-extension/src/main/java/us/codecraft/webmagic/downloader/PhantomJSDownloader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class PhantomJSDownloader extends AbstractDownloader {
2020
private static Logger logger = LoggerFactory.getLogger(PhantomJSDownloader.class);
2121
private static String crawlJsPath;
2222
private static String phantomJsCommand = "phantomjs"; // default
23+
private static final String HTTP_REQUEST_ERROR = "HTTP request failed";
2324

2425
private int retryNum;
2526
private int threadNum;
@@ -91,14 +92,14 @@ public Page download(Request request, Task task) {
9192
logger.info("downloading page: " + request.getUrl());
9293
}
9394
String content = getPage(request);
94-
if (content.contains("HTTP request failed")) {
95+
if (content.contains(HTTP_REQUEST_ERROR)) {
9596
for (int i = 1; i <= getRetryNum(); i++) {
9697
content = getPage(request);
97-
if (!content.contains("HTTP request failed")) {
98+
if (!content.contains(HTTP_REQUEST_ERROR)) {
9899
break;
99100
}
100101
}
101-
if (content.contains("HTTP request failed")) {
102+
if (content.contains(HTTP_REQUEST_ERROR)) {
102103
//when failed
103104
Page page = new Page();
104105
page.setRequest(request);

0 commit comments

Comments
 (0)