zoukankan      html  css  js  c++  java
  • HTTP Client工具类


    HTTP Client工具类:




    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;

    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;

    public class HTTPSample {

    private static CloseableHttpClient httpClient = null;

    public static CloseableHttpClient httpClient(){
    if(httpClient!=null) return HTTPSample.httpClient;
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // Increase max total connection to 200
    cm.setMaxTotal(200);
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);
    HTTPSample.httpClient = HttpClients.custom().setConnectionManager(cm).build();
    return httpClient;
    }

    public static String httpGet(String url, Map<String, String> requestParams) {

    HttpGet httpGet = null;
    String result = "";
    try {
    // ��������
    StringBuilder builder = new StringBuilder(url);
    builder.append("?");
    for (Map.Entry<String, String> entry : requestParams.entrySet()) {
    builder.append((String) entry.getKey());
    builder.append("=");
    builder.append((String) entry.getValue());
    builder.append("&");
    }

    String tmpUrl = builder.toString();
    tmpUrl = tmpUrl.substring(0, tmpUrl.length() - 1);

    httpGet = new HttpGet(tmpUrl);

    // System.out.println("executing request " + httpGet.getURI());
    // System.out.println("-------------------------------------");

    HttpResponse response = httpClient().execute(httpGet);

    // reponse header
    // System.out.println(response.getStatusLine().getStatusCode());

    Header[] headers = response.getAllHeaders();
    // for (Header header : headers) {
    // System.out.println(header.getName() + ": " + header.getValue());
    // }

    // System.out.println();

    // ��ҳ����
    HttpEntity httpEntity = response.getEntity();
    // System.out.println(EntityUtils.toString(httpEntity));
    result = EntityUtils.toString(httpEntity);
    } catch (ClientProtocolException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (httpGet != null) {
    httpGet.abort();
    }
    }
    return result;
    }

    public static String httpPost(String url, Map<String, String> requestParams, String urlEncode) {

    HttpPost httpPost = null;
    String result = "";
    try {
    // ��������
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    for (Map.Entry<String, String> entry : requestParams.entrySet()) {
    params.add(new BasicNameValuePair((String) entry.getKey(),
    (String) entry.getValue()));
    }

    httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params, urlEncode));

    // System.out.println("executing request " + httpPost.getURI());
    // System.out.println("-------------------------------------");

    // reponse header
    HttpResponse response = httpClient().execute(httpPost);
    // System.out.println(response.getStatusLine().getStatusCode());

    Header[] headers = response.getAllHeaders();
    // for (Header header : headers) {
    // System.out.println(header.getName() + ": " + header.getValue());
    // }

    // System.out.println();

    // ��ҳ����
    HttpEntity httpEntity = response.getEntity();
    result = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (ClientProtocolException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (httpPost != null) {
    httpPost.abort();
    }
    }
    return result;
    }
    }
  • 相关阅读:
    SQL Server 2005技术内幕:查询、调整和优化2——Bookmark Lookup
    隐藏文字用图片代替方案
    检索get参素
    a:hover之后ie6要恢复原来的状态需要hover的时候改变a的状态
    暂记
    台式机搭建wifi热点
    多行文本垂直居中
    chrome上做web app开发 模拟不同的分辨率和设备
    工厂模式和构造函数
    字符串替换
  • 原文地址:https://www.cnblogs.com/jiuchongxiao/p/5633487.html
Copyright © 2011-2022 走看看