weixin-java-tools/src/main/java/chanjarster/weixin/util/http/Utf8ResponseHandler.java

33 lines
1.1 KiB
Java
Raw Normal View History

2014-08-24 14:18:31 +08:00
package chanjarster.weixin.util.http;
2014-08-21 22:13:13 +08:00
import java.io.IOException;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;
/**
* copy from {@link org.apache.http.impl.client.BasicResponseHandler}
* @author chanjarster
*
*/
public class Utf8ResponseHandler implements ResponseHandler<String> {
2014-08-21 22:13:13 +08:00
public static final ResponseHandler<String> INSTANCE = new Utf8ResponseHandler();
2014-08-21 22:13:13 +08:00
public String handleResponse(final HttpResponse response) throws HttpResponseException, IOException {
final StatusLine statusLine = response.getStatusLine();
final HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() >= 300) {
EntityUtils.consume(entity);
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
return entity == null ? null : EntityUtils.toString(entity, Consts.UTF_8);
}
}