httpclient之post 方法(参数为string)
Posted chenzx0918
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient之post 方法(参数为string)相关的知识,希望对你有一定的参考价值。
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/18.
*/
public class HttpclientPostForString {
public static void main(String[] args) {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
//配置超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.setRedirectsEnabled(true)
.build();
HttpPost httpPost = new HttpPost("https://passport.cnblogs.com/user/signin?ReturnUrl=https%3A%2F%2Fwww.cnblogs.com%2F");
//设置超时时间
httpPost.setConfig(requestConfig);
//装配post请求参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("name", "jiangcui"));
list.add(new BasicNameValuePair("password", "chenzx0918"));
try {
//将参数进行编码为合适的格式,如将键值对编码为param1=value1¶m2=value2
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "utf-8");
httpPost.setEntity(urlEncodedFormEntity);
//执行post方法
CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost);
String strResult = "";
if (null != closeableHttpResponse && !"".equals(closeableHttpResponse)) {
System.out.println(closeableHttpResponse.getStatusLine().getStatusCode());
if (closeableHttpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = closeableHttpResponse.getEntity();
strResult = EntityUtils.toString(httpEntity);
System.out.println(strResult);
//将字符串转换为 JSON格式
JSONObject jsonObject = new JSONObject(strResult);
//获取json串中的数据
//String str = jsonObject.get("name").toString;
} else {
strResult = "Error Response:" + closeableHttpResponse.getStatusLine().getStatusCode();
System.out.println(strResult);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (closeableHttpClient != null) {
closeableHttpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上是关于httpclient之post 方法(参数为string)的主要内容,如果未能解决你的问题,请参考以下文章
HttpURLConnection和HttpClient使用
HttpClient 的GET(带参数)POST请求方式,工具类方法