• 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;
    }
    }
  • 相关阅读:
    python requests 模拟登陆网站,抓取数据
    python 爬取淘宝的模特照片
    vim 和grep 正则表达式相似和区别
    python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
    python 正则表达式
    12个球,其中一个和其他的重量不一样,有一个天平,最多几次找出这个球
    25匹马中选出跑得最快的3匹,每次只有5匹马同时跑,最少要比赛几次
    1000瓶药水,1瓶有毒药,几只小白鼠能够找出毒药
    146 LRU Cache
    用两个int值实现读写锁
  • 原文地址:https://www.cnblogs.com/jiuchongxiao/p/5633487.html
Copyright © 2020-2023  润新知