zoukankan      html  css  js  c++  java
  • httpclient之post 方法(参数为Map类型)

    package httpclient;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import org.apache.http.*;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.config.RequestConfig;
    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.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;

    import org.json.JSONObject;

    /**
    * Created by jiangcui on 2018/5/21.
    */
    public class HttpClientPostForMap {
    public static void main(String[] args) {

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("username", "jiangcui");
    map.put("password", "chenzx0918");

    String url = "https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F";
    String result = HttpClientPostForMap.doPost(url, map);

    System.out.println(result);


    }

    // 静态方法,类名可直接调用
    public static String doPost(String url, Map<String, Object> paramsMap) {
    CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
    //配置连接超时时间
    RequestConfig requestConfig = RequestConfig.custom()
    .setConnectTimeout(5000)
    .setConnectionRequestTimeout(5000)
    .setSocketTimeout(5000)
    .setRedirectsEnabled(true)
    .build();
    HttpPost httpPost = new HttpPost(url);
    //设置超时时间
    httpPost.setConfig(requestConfig);

    //装配post请求参数
    List<NameValuePair> list = new ArrayList<NameValuePair>();
    for (String key : paramsMap.keySet()) {
    list.add(new BasicNameValuePair(key, String.valueOf(paramsMap.get(key))));
    }

    try {
    //将参数进行编码为合适的格式,如将键值对编码为param1=value1&param2=value2
    UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "utf-8");
    httpPost.setEntity(urlEncodedFormEntity);

    //执行 post请求
    CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost);
    String strRequest = "";
    if (null != closeableHttpResponse && !"".equals(closeableHttpResponse)) {
    System.out.println(closeableHttpResponse.getStatusLine().getStatusCode());
    if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    HttpEntity httpEntity = closeableHttpResponse.getEntity();
    strRequest = EntityUtils.toString(httpEntity);
    } else {
    strRequest = "Error Response" + closeableHttpResponse.getStatusLine().getStatusCode();
    }
    }
    return strRequest;

    } catch (ClientProtocolException e) {
    e.printStackTrace();
    return "协议异常";
    } catch (ParseException e) {
    e.printStackTrace();
    return "解析异常";
    } catch (IOException e) {
    e.printStackTrace();
    return "传输异常";
    } finally {
    try {
    if (closeableHttpClient != null) {
    closeableHttpClient.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    }

    }
  • 相关阅读:
    Leetcode 811. Subdomain Visit Count
    Leetcode 70. Climbing Stairs
    Leetcode 509. Fibonacci Number
    Leetcode 771. Jewels and Stones
    Leetcode 217. Contains Duplicate
    MYSQL安装第三步报错
    .net 开发WEB程序
    JDK版本问题
    打开ECLIPSE 报failed to load the jni shared library
    ANSI_NULLS SQL语句
  • 原文地址:https://www.cnblogs.com/jshtest/p/9070699.html
Copyright © 2011-2022 走看看