post请求的参数如何添加?
post请求的参数如何添加?老师的视频中只是一个字符串,能否帮忙指出如何添加post方法携带的参数?谢谢老师
post请求的参数如何添加?老师的视频中只是一个字符串,能否帮忙指出如何添加post方法携带的参数?谢谢老师
2018-02-07
public static String httpPost(String url,List<BasicNameValuePair> list) {
String result = null;
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(5000) //设置连接超时时间
.setConnectionRequestTimeout(5000) // 设置请求超时时间
.setSocketTimeout(5000)
.setRedirectsEnabled(true)//默认允许自动重定向
.build();
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8");
//设置post请求参数
httpPost.setEntity(entity);
HttpResponse httpResponse = closeableHttpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
举报