httpclient怎么设置post请求长度

发布网友 发布时间:2022-04-19 12:44

我来回答

3个回答

热心网友 时间:2023-06-27 13:27

HttpClient 是apache 组织下面的一个用于处理HTTP 请求和响应的开源工具。所用jar包为httpclient-4.3.6.jar、httpcore-4.3.3.jar、httpmime-4.3.6.jar、commons-codec-1.6.jar。
发送Post请求代码如下:
[java] view plain copy
package com.zkn.newlearn.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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 com.google.common.collect.Lists;

/**
*
* @author zkn 2016-06-05
*
*/
public class HttpClientTest01 {

public static void main(String[] args) {
//创建HttpClient对象
CloseableHttpClient closeHttpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
//发送Post请求
HttpPost httpPost = new HttpPost("http://localhost:8080/MyWebxTest/getCityByProvinceEname.do");
//设置Post参数
List<NameValuePair> params = Lists.newArrayList();
params.add(new BasicNameValuePair("cityEname", "henan"));
try {
//转换参数并设置编码格式
httpPost.setEntity(new UrlEncodedFormEntity(params,Consts.UTF_8));
//执行Post请求 得到Response对象
httpResponse = closeHttpClient.execute(httpPost);
//httpResponse.getStatusLine() 响应头信息
System.out.println(httpResponse.getStatusLine());
//返回对象 向上造型
HttpEntity httpEntity = httpResponse.getEntity();
if(httpEntity != null){
//响应输入流
InputStream is = httpEntity.getContent();
//转换为字符输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is,Consts.UTF_8));
String line = null;
while((line=br.readLine())!=null){
System.out.println(line);
}
//关闭输入流
is.close();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(httpResponse != null){
try {
httpResponse.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(closeHttpClient != null){
try {
closeHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

热心网友 时间:2023-06-27 13:27

String content = JSONBinder.binder(Vendor.class).toJSON(v); 不要转 string 直接发对象

热心网友 时间:2023-06-27 13:28

String content

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
13.847609s