httpclient之post 方法(参数为json类型)

Posted chenzx0918

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient之post 方法(参数为json类型)相关的知识,希望对你有一定的参考价值。

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.entity.ContentType;
import org.apache.http.entity.StringEntity;
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 HttpClientPostForJson {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.append("username", "jiangcui");
jsonObject.append("password", "pdmi1234");

String url = "https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F";
String result = HttpClientPostForJson.doPostForJson(url,jsonObject);
System.out.println(result);
}

//静态方法,可直接调用
public static String doPostForJson(String url, JSONObject jsonParams) {
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);
httpPost.setHeader("Content-Type", "application/json");

//JSONObject jsonObject = null;

try {
StringEntity entity = new StringEntity(jsonParams.toString(), "utf-8");
entity.setContentEncoding("utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);

//执行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();
}
}
}
}

以上是关于httpclient之post 方法(参数为json类型)的主要内容,如果未能解决你的问题,请参考以下文章

httpclient之post 方法(参数为Map类型)

HttpClient POST/SET方法

HttpClient 的GET(带参数)POST请求方式,工具类方法

HttpClient使用post方法提交数据后服务端如何获得这些参数

在 Android 中使用带有 post 参数的 HttpClient 和 HttpPost

使用 POST 方法将 HttpWebRequest 转换为 HttpClient