WeixinResponse构造器调整

This commit is contained in:
jinyu 2015-09-10 21:44:55 +08:00
parent bb18b98538
commit 8de4f4414d
3 changed files with 20 additions and 34 deletions

View File

@ -105,6 +105,7 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
HttpHeaders headers = request.getHeaders(); HttpHeaders headers = request.getHeaders();
if (headers == null) { if (headers == null) {
headers = new HttpHeaders(); headers = new HttpHeaders();
}
if (!headers.containsKey(HttpHeaders.HOST)) { if (!headers.containsKey(HttpHeaders.HOST)) {
headers.set(HttpHeaders.HOST, request.getURI().getHost()); headers.set(HttpHeaders.HOST, request.getURI().getHost());
} }
@ -116,7 +117,6 @@ public class SimpleHttpClient extends AbstractHttpClient implements HttpClient {
if (!headers.containsKey(HttpHeaders.USER_AGENT)) { if (!headers.containsKey(HttpHeaders.USER_AGENT)) {
headers.set(HttpHeaders.USER_AGENT, "jdk/httpclient"); headers.set(HttpHeaders.USER_AGENT, "jdk/httpclient");
} }
}
for (Iterator<Entry<String, List<String>>> headerIterator = headers for (Iterator<Entry<String, List<String>>> headerIterator = headers
.entrySet().iterator(); headerIterator.hasNext();) { .entrySet().iterator(); headerIterator.hasNext();) {
Entry<String, List<String>> header = headerIterator.next(); Entry<String, List<String>> header = headerIterator.next();

View File

@ -12,7 +12,6 @@ import com.foxinmy.weixin4j.http.HttpMethod;
import com.foxinmy.weixin4j.http.HttpParams; import com.foxinmy.weixin4j.http.HttpParams;
import com.foxinmy.weixin4j.http.HttpRequest; import com.foxinmy.weixin4j.http.HttpRequest;
import com.foxinmy.weixin4j.http.HttpResponse; import com.foxinmy.weixin4j.http.HttpResponse;
import com.foxinmy.weixin4j.http.HttpStatus;
import com.foxinmy.weixin4j.http.apache.FormBodyPart; import com.foxinmy.weixin4j.http.apache.FormBodyPart;
import com.foxinmy.weixin4j.http.apache.HttpMultipartMode; import com.foxinmy.weixin4j.http.apache.HttpMultipartMode;
import com.foxinmy.weixin4j.http.apache.MultipartEntity; import com.foxinmy.weixin4j.http.apache.MultipartEntity;
@ -97,10 +96,8 @@ public class WeixinRequestExecutor {
request.setParams(params); request.setParams(params);
try { try {
HttpResponse httpResponse = httpClient.execute(request); HttpResponse httpResponse = httpClient.execute(request);
HttpStatus status = httpResponse.getStatus();
HttpHeaders headers = httpResponse.getHeaders(); HttpHeaders headers = httpResponse.getHeaders();
WeixinResponse response = new WeixinResponse(headers, status, WeixinResponse response = new WeixinResponse(httpResponse);
httpResponse.getBody());
String contentType = headers.getFirst(HttpHeaders.CONTENT_TYPE); String contentType = headers.getFirst(HttpHeaders.CONTENT_TYPE);
String disposition = headers String disposition = headers
.getFirst(HttpHeaders.CONTENT_DISPOSITION); .getFirst(HttpHeaders.CONTENT_DISPOSITION);

View File

@ -1,14 +1,13 @@
package com.foxinmy.weixin4j.http.weixin; package com.foxinmy.weixin4j.http.weixin;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.http.HttpHeaders; import com.foxinmy.weixin4j.http.HttpHeaders;
import com.foxinmy.weixin4j.http.HttpResponse;
import com.foxinmy.weixin4j.http.HttpStatus; import com.foxinmy.weixin4j.http.HttpStatus;
import com.foxinmy.weixin4j.util.IOUtil;
import com.foxinmy.weixin4j.util.StringUtil; import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.xml.XmlStream; import com.foxinmy.weixin4j.xml.XmlStream;
@ -18,15 +17,10 @@ public class WeixinResponse {
private boolean isXmlResult; private boolean isXmlResult;
private volatile String text; private volatile String text;
private final HttpHeaders headers; private final HttpResponse response;
private final HttpStatus status;
private final InputStream body;
public WeixinResponse(HttpHeaders headers, HttpStatus status, public WeixinResponse(HttpResponse response) {
InputStream body) { this.response = response;
this.headers = headers;
this.status = status;
this.body = body;
} }
public void setJsonResult(boolean isJsonResult) { public void setJsonResult(boolean isJsonResult) {
@ -39,12 +33,7 @@ public class WeixinResponse {
public String getAsString() { public String getAsString() {
if (text == null) { if (text == null) {
try { text = StringUtil.newStringUtf8(response.getContent());
text = StringUtil.newStringUtf8(IOUtil.toByteArray(body));
} catch (IOException e) {
e.printStackTrace();
;
}
} }
return text; return text;
} }
@ -78,14 +67,14 @@ public class WeixinResponse {
} }
public HttpHeaders getHeaders() { public HttpHeaders getHeaders() {
return headers; return response.getHeaders();
} }
public HttpStatus getStatus() { public HttpStatus getStatus() {
return status; return response.getStatus();
} }
public InputStream getBody() { public InputStream getBody() {
return body; return response.getBody();
} }
} }