import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;
public class HttpClient { public static String get(String url) { return get(url, null, null, "UTF-8");
public static String post(String url, Map<String, String> form) { return post(url, form, null, "UTF-8");
public static String post(String url, String jsonForm) { return postJson(url, jsonForm, null, "UTF-8");
public static String get(String url, Map<String, String> headerMap, Map<String, String> paramMap, String codeType) { CloseableHttpClient httpClient = HttpClients.createDefault();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : paramMap.entrySet()) { pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
CloseableHttpResponse response = null;
URIBuilder builder = new URIBuilder(url);
builder.setParameters(pairs);
HttpRequestBase httpRequestBase = new HttpGet(builder.build());
headerMap.forEach((name, value) -> { httpRequestBase.removeHeaders(name);
httpRequestBase.addHeader(name, value);
httpRequestBase.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)"); response = httpClient.execute(httpRequestBase);
response.setHeader("Content-type", "text/html;charset=UTF-8"); if (response != null && response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity();
result = entityToString(entity, codeType);
} catch (URISyntaxException e) { } catch (ClientProtocolException e) { } catch (IOException e) { } catch (IOException e) { public static String post(String url, Map<String, String> form, Map<String, String> header, String codeType) { CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : form.entrySet()) { pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
CloseableHttpResponse response = null;
post.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
for (Map.Entry<String, String> entry : header.entrySet()) { post.addHeader(entry.getKey(), entry.getValue());
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity();
result = entityToString(entity, codeType);
} catch (UnsupportedEncodingException e) { } catch (ClientProtocolException e) { } catch (IOException e) { } catch (IOException e) { public static String postJson(String url, String jsonString, Map<String, String> header, String codeType) { CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(url);
CloseableHttpResponse response = null;
post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8"))); for (Map.Entry<String, String> entry : header.entrySet()) { post.addHeader(entry.getKey(), entry.getValue());
response = httpClient.execute(post);
if (response != null && response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity();
result = entityToString(entity, codeType);
} catch (UnsupportedEncodingException e) { } catch (ClientProtocolException e) { } catch (IOException e) { } catch (IOException e) { private static String entityToString(HttpEntity entity, String codeType) throws IOException { long lenth = entity.getContentLength();
if (lenth != -1 && lenth < 2048) { result = EntityUtils.toString(entity, codeType);
InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
CharArrayBuffer buffer = new CharArrayBuffer(2048);
char[] tmp = new char[1024];
while ((l = reader1.read(tmp)) != -1) { buffer.append(tmp, 0, l);
result = buffer.toString();