From c3bdb204580cf7618bf70e67992ab14e227156dd Mon Sep 17 00:00:00 2001 From: "yihua.huang" Date: Sat, 22 Jul 2017 11:49:23 +0800 Subject: [PATCH] #631 remove IllegalArgumentException of HttpRequestBody.json and so on --- .../webmagic/model/HttpRequestBody.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/webmagic-core/src/main/java/us/codecraft/webmagic/model/HttpRequestBody.java b/webmagic-core/src/main/java/us/codecraft/webmagic/model/HttpRequestBody.java index abd3d5bd..7d3b3078 100644 --- a/webmagic-core/src/main/java/us/codecraft/webmagic/model/HttpRequestBody.java +++ b/webmagic-core/src/main/java/us/codecraft/webmagic/model/HttpRequestBody.java @@ -64,24 +64,36 @@ public class HttpRequestBody implements Serializable { this.encoding = encoding; } - public static HttpRequestBody json(String json, String encoding) throws UnsupportedEncodingException { - return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding); + public static HttpRequestBody json(String json, String encoding) { + try { + return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding); + } catch (UnsupportedEncodingException e) { + throw new IllegalArgumentException("illegal encoding " + encoding, e); + } } - public static HttpRequestBody xml(String xml, String encoding) throws UnsupportedEncodingException { - return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding); + public static HttpRequestBody xml(String xml, String encoding) { + try { + return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding); + } catch (UnsupportedEncodingException e) { + throw new IllegalArgumentException("illegal encoding " + encoding, e); + } } - public static HttpRequestBody custom(byte[] body, String contentType, String encoding) throws UnsupportedEncodingException { + public static HttpRequestBody custom(byte[] body, String contentType, String encoding) { return new HttpRequestBody(body, contentType, encoding); } - public static HttpRequestBody form(Map params, String encoding) throws UnsupportedEncodingException { + public static HttpRequestBody form(Map params, String encoding){ List nameValuePairs = new ArrayList(params.size()); for (Map.Entry entry : params.entrySet()) { nameValuePairs.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()))); } - return new HttpRequestBody(URLEncodedUtils.format(nameValuePairs, encoding).getBytes(encoding), ContentType.FORM, encoding); + try { + return new HttpRequestBody(URLEncodedUtils.format(nameValuePairs, encoding).getBytes(encoding), ContentType.FORM, encoding); + } catch (UnsupportedEncodingException e) { + throw new IllegalArgumentException("illegal encoding " + encoding, e); + } } public byte[] getBody() {