HttpClient 发送HTTP请求
2016-05-17 12:22
447 查看
HttpClient 发送HTTP请求
需要Apache的三个包:
commons-httpclient-3.1.jar
commons-codec-1.9.jar
commons-logging.jar
没有自己在Apache官网下载,怎么导入就不多说了
代码如下:
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class MyTest {
/**
* HttpClient HTTP发送
*/
public static void main(String[] args) {
//定义发送状态
int statusCode = 0;
//定义响应接收流
InputStream in=null;
//
FileOutputStream fop = null;
//目标URL
String targetUrl = "http://localhost:8090/MyTest/index2.jsp";
try{
//定义HttpClient
HttpClient client = new HttpClient();
//Post模式提交
PostMethod method = new PostMethod(targetUrl);
//设置参数s
NameValuePair[] data = { new NameValuePair("name","leijianwei"),new NameValuePair("pwd","123456")};
//装入参数
method.setRequestBody(data);
//执行模式发送,并获取发送状态
statusCode = client.executeMethod(method);
//判断发送状态是否是200
if(statusCode == HttpStatus.SC_OK){
//获取服务端响应
in = method.getResponseBodyAsStream();
//fop =new FileOutputStream(new File(fileName));//存入文件
byte []binRead=new byte[1024];
int b = -1;
//循环读取流
while((b=in.read(binRead))!=-1){
//打印
String responseBody = new String(binRead,"utf-8");
System.out.println("打印出来吧:"+responseBody.trim());
//存入文件
//fop.write(binRead,0,b);
}
}else{
}
}catch(Exception e){
e.printStackTrace();
}
}
}
需要Apache的三个包:
commons-httpclient-3.1.jar
commons-codec-1.9.jar
commons-logging.jar
没有自己在Apache官网下载,怎么导入就不多说了
代码如下:
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class MyTest {
/**
* HttpClient HTTP发送
*/
public static void main(String[] args) {
//定义发送状态
int statusCode = 0;
//定义响应接收流
InputStream in=null;
//
FileOutputStream fop = null;
//目标URL
String targetUrl = "http://localhost:8090/MyTest/index2.jsp";
try{
//定义HttpClient
HttpClient client = new HttpClient();
//Post模式提交
PostMethod method = new PostMethod(targetUrl);
//设置参数s
NameValuePair[] data = { new NameValuePair("name","leijianwei"),new NameValuePair("pwd","123456")};
//装入参数
method.setRequestBody(data);
//执行模式发送,并获取发送状态
statusCode = client.executeMethod(method);
//判断发送状态是否是200
if(statusCode == HttpStatus.SC_OK){
//获取服务端响应
in = method.getResponseBodyAsStream();
//fop =new FileOutputStream(new File(fileName));//存入文件
byte []binRead=new byte[1024];
int b = -1;
//循环读取流
while((b=in.read(binRead))!=-1){
//打印
String responseBody = new String(binRead,"utf-8");
System.out.println("打印出来吧:"+responseBody.trim());
//存入文件
//fop.write(binRead,0,b);
}
}else{
}
}catch(Exception e){
e.printStackTrace();
}
}
}
相关文章推荐
- 网络安全学习和CTF必不可少的一些网站
- 网络I/O中的同步、异步、阻塞和非阻塞概念
- Android-async-http-1.4.9版本去除了httpclient的东西,所以会出现Header[] headers找不到的错误
- 关于scrapy网络爬虫的xpath书写经验总结
- VMware Workstation学习之路——网络设置——桥接连接方式
- IOS开发笔记 - 基于SDWebImage的网络图片加载处理
- Windows平台基于Caffe框架的LeNet网络训练
- HTTP响应头和请求头信息对照表
- TCP三次握手,四次分手详细解析
- HTTP协议的特点
- TCP中的MSS解读
- HTTP HSTS协议和 nginx
- UNIX网络编程笔记(1)—传输层协议
- OkHttp各种请求方法
- Android程序员做了一个导航网站 http://loseronline.cn/
- 在使用 HttpWebRequest Post数据时候返回 400错误
- Android中进行基于HTTP协议的网络访问基础
- tcpdump 抓包存文件使用tftp传输 wireshark 打开报错
- http-关于application/x-www-form-urlencoded等字符编码的解释说明
- TCP状态以及关于内核调优的几个参数