All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.chanjar.weixin.common.util.http.Utf8ResponseHandler Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package me.chanjar.weixin.common.util.http;

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 Daniel Qian
 *
 */
public class Utf8ResponseHandler implements ResponseHandler {

  public static final ResponseHandler INSTANCE = new Utf8ResponseHandler();
  
  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);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy