import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
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;
public void requestToBaiDu() {
//发送请求的URL
String url = "http://www.baidu.com";
//编码格式
String charset = "UTF-8";
//请求内容
String content = "{"date":"2017-11-06 18:27:10.0","sum":"-3400","complainOdrNbr":"null"}";//使用帮助类HttpClients创建CloseableHttpClient对象.
CloseableHttpClient client = HttpClients.createDefault();
//HTTP请求类型创建HttpPost实例
HttpPost post = new HttpPost(url);
//使用addHeader方法添加请求头部,诸如User-Agent, Accept-Encoding等参数.
post.setHeader("Content-Type", "application/json;charset=UTF-8");
// 组织数据StringEntity se = new StringEntity(content );
//设置编码格式
se.setContentEncoding(charset);
//设置数据类型
se.setContentType("application/json");
//对于POST请求,把请求体填充进HttpPost实体.
post.setEntity(se);
//通过执行HttpPost请求获取CloseableHttpResponse实例 ,从此CloseableHttpResponse实例中获取状态码,错误信息,以及响应页面等等.
CloseableHttpResponse response = client.execute(post);
//通过HttpResponse接口的getEntity方法返回响应信息,并进行相应的处理
HttpEntity entity = response.getEntity();
String resData = EntityUtils.toString(response.getEntity());
//最后关闭HttpClient资源.
httpClient.close()
}
所需jar包:httpclient-4.5.2.jar,httpcore-4.4.3.jar。可到maven资源库下载