private String sendBasic(HttpRequestBase request) throws HttpException { try { HttpResponse response = httpClient.execute(request); HttpEntity entity = response.getEntity(); String body = ""; if (entity != null) { body = EntityUtils.toString(entity, UTF_8); if (entity.getContentType() == null) { body = new String(body.getBytes(ISO_8859_1), UTF_8); } } int code = response.getStatusLine().getStatusCode(); if (code < 200 || code >= 300) { throw new Exception(String.format(" code : '%s' , body : '%s'", code, body)); } return body; } catch (Exception ex) { throw new HttpException( "Fail to send " + request.getMethod() + " request to url " + request.getURI() + ", " + ex.getMessage(), ex); } finally { request.releaseConnection(); } }
/** get Charset String From HTTP Response */ private String getCharsetFromEntity(HttpEntity entity, String defCharset) { final Header header = entity.getContentType(); if (header != null) { final HeaderElement[] elements = header.getElements(); if (elements.length > 0) { HeaderElement helem = elements[0]; // final String mimeType = helem.getName(); final NameValuePair[] params = helem.getParameters(); if (params != null) { for (final NameValuePair param : params) { if (param.getName().equalsIgnoreCase("charset")) { String s = param.getValue(); if (s != null && s.length() > 0) { return s; } } } } } } return defCharset == null ? Charsets.UTF_8 : defCharset; }
@Override public Header getContentType() { return delegate.getContentType(); }