HTTP Post Request using Apache Commons
2023-09-11 14:17:10 时间
Demonstrates an HTTP Post using the Apache Commons HTTP library.
Required Libraries:
- httpcore-4.1.jar
- commons-io-2.4.jar
- commons-logging-1.1.1.jar
- httpmime-4.1.1.jar
- httpclient-4.1.1.jar
Example Source Code
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.apache.commons.io.IOUtils; 7 import org.apache.http.HttpEntity; 8 import org.apache.http.HttpResponse; 9 import org.apache.http.client.ClientProtocolException; 10 import org.apache.http.client.HttpClient; 11 import org.apache.http.client.entity.UrlEncodedFormEntity; 12 import org.apache.http.client.methods.HttpPost; 13 import org.apache.http.impl.client.DefaultHttpClient; 14 import org.apache.http.message.BasicNameValuePair; 15 16 public class ExampleHttpPost 17 { 18 public static void main(String args[]) throws ClientProtocolException, IOException 19 { 20 HttpPost httppost = new HttpPost("https://stanfordwho.stanford.edu/SWApp/Search.do"); 21 22 List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); 23 parameters.add(new BasicNameValuePair("search", "jsproch")); 24 parameters.add(new BasicNameValuePair("filters", "closed")); 25 parameters.add(new BasicNameValuePair("affilfilter", "everyone")); 26 parameters.add(new BasicNameValuePair("btnG", "Search")); 27 28 httppost.setEntity(new UrlEncodedFormEntity(parameters)); 29 30 HttpClient httpclient = new DefaultHttpClient(); 31 HttpResponse httpResponse = httpclient.execute(httppost); 32 HttpEntity resEntity = httpResponse.getEntity(); 33 34 // Get the HTTP Status Code 35 int statusCode = httpResponse.getStatusLine().getStatusCode(); 36 37 // Get the contents of the response 38 InputStream input = resEntity.getContent(); 39 String responseBody = IOUtils.toString(input); 40 input.close(); 41 42 // Print the response code and message body 43 System.out.println("HTTP Status Code: "+statusCode); 44 System.out.println(responseBody); 45 } 46 }
相关文章
- 【Http协议】深入理解HTTP协议
- Apache MINA (3) NioSocketAcceptor初始化
- Apache MINA (1) 简介
- Apache prefork 模块指令分析
- CentOS Linux系统下更改Apache默认网站目录
- 【异常(待解决)】org.apache.http.NoHttpResponseException: api.weixin.qq.com:443 failed to respond
- Apache http 包中的常量
- Apache Shiro
- Scala Http 请求调用 - scalaj-http
- Apache服务器虚拟主机(vhost)常见配置
- linux下安装apache(httpd-2.4.3版本)各种坑
- Maven 3.8.1新特性:默认屏蔽外部 HTTP 资源库(会导致repo或mirror地址为http时报错)
- 在Apache设置虚拟机
- 使用 http-proxy 代理 SAP UI5 应用发起的 HTTP 请求
- 使用 http-proxy 代理 HTTP 请求时遇到的 the requested url is invalid 错误消息
- Atitit 返回http500返回码,以及自定义返回提示 目录 1. 可以看到从php发送http响应代码的三种方法:1 1.1. Header("HTTP/1.1 404 Not Found"
- Atitit 微服务实践 艾提拉著 微服务主要解决几个问题负载均很 目录 1. 微服务的模式 http请求层 vs服务层1 1.1. Http vs 服务层优缺点1 2. 实现技术2
- 【实施工程师】Wampserver64橙色无法启动【apache】问题
- Cause: org.apache.ibatis.type.TypeException: Error setting non null for xxx with JdbcType错误的详细解决方法
- maven项目建立pom.xml报无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3
- exception: java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused
- 【云原生 | 31】Docker运行实时流计算框架Apache Storm
- MyBatis 本是apache的一个开源项目iBatis
- 【Web开发】使用Apache搭建Http下载服务器
- 百万计Topic,Apache Pulsar 在腾讯云的稳定性优化实践