HttpURLConnection模拟浏览器http请求

本文是参考网络文章测试整理,报歉忘记参考地址了,要是作者有意见就请联系我.文中只是对程序的运行做了测试,但理论没有去考证正确性,等后面研究下再完善.这只是做参考

代码:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
* HttpURLConnection 模拟浏览器http请求处理测试
*
* 整体操作流程(注意顺序)
* 1.根据url地址创建URL对象
* 2.url.openConnection() 得到HttpURLConnection
* 3.设置HttpURLConnection的配置(会根据配置生成http请求头信息)
* 4.httpConn.connect()建立与服务器的TCP连接
* 5.通过httpConn得到输出流,并写入参数
* 6.通过httpConn得到输入流,并读取服务器返回信息(这时才真正产生http请求发送给服务器,但可以根据配置来改变;一般返回可能是html字符串)
*
* httpRequestPost/httpRequestGet两种方式测试通过,返回的是html字符串(后台是jsp)
*
*/
public class HttpURLConnectionTest {

public static void main(String[] strs) throws Exception{
HttpURLConnectionTest t = new HttpURLConnectionTest();
String url = "http://localhost:8080/URLTest/filesystem/fileBrowse.do";
t.httpUrlConnection(url);
}

private void httpUrlConnection(String urlStr) throws Exception{
httpRequestGet(urlStr);
httpRequestPost(urlStr);
}

/**
* http post请求
* @param urlStr URL地址
* @throws Exception
*/
private void httpRequestPost(String urlStr) throws Exception{
URL url = new URL(urlStr);

HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();

//设置是否向connection输出,因为是post请求,参数要放在 http正文内,因此需要设为true
httpConn.setDoOutput(true);

//是否向connection输入,默认为true
httpConn.setDoInput(true);

//POST请求方式
httpConn.setRequestMethod("POST");

//post请求不能使用缓存
httpConn.setUseCaches(false);

//是否自动执行重定向,默认为true
httpConn.setInstanceFollowRedirects(true);

//配置本次连接的Content-type,配置为application/x-www-form-urlencoded的意思是正文是urlencoded编码过的form参数
//下面我们可以看到我们对正文内容使用URLEncoder.encode 进行编码
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

//连接,从openConnection()至此的配置必须要在connect之前完成, 注意的是connection.getOutputStream会隐含的进行connect。
httpConn.connect();

DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());

//发送正文,正文内容其实跟get的URL中'?'后的参数字符串一致 word=wait&tn=news&from=news
String content = "filePath=" + URLEncoder.encode("设计资料/设计", "utf-8");

//DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
dos.writeBytes(content);

//刷新流
dos.flush();

//关闭流
dos.close();

// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));

System.out.println("=========post request接收数据内容开始============");
String lines;
while ((lines = reader.readLine()) != null) {
System.out.println(lines);
}
reader.close();
System.out.println("=========post request接收数据内容结束============");
httpConn.disconnect();
}

/**
* http get 请求
* @param urlStr 请求URL地址
* @throws Exception
*/
private void httpRequestGet(String urlStr) throws Exception{
//URL拼接,如:"http://www.baidu.com?name=HI,中国",这里对特殊字符进行了编码,不然会产生乱码
URL url = new URL(urlStr + "?filePath=" + URLEncoder.encode("设计资料/设计", "utf-8"));

//openConnection函数会根据URL的协议返回不同的URLConnection子类的对象
//这里URL是一个http,因此实际返回的是HttpURLConnection
HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();

//进行连接,实际上request要在下一句的connection.getInputStream()函数中才会真正发到 服务器****待验证
httpConn.connect();

// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));

System.out.println("=========get request接收数据内容开始============");
String lines;
while ((lines = reader.readLine()) != null) {
System.out.println(lines);
}
reader.close();
System.out.println("=========get request接收数据内容结束============");
httpConn.disconnect();
}


/**
* http post 请求(未测试)
*
* @param urlStr
* @throws Exception
*/
public static void httpRequestPostBig(String urlStr) throws Exception {
URL postUrl = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//与httpRequestPost()的不同,设置了块大小为5字节
connection.setChunkedStreamingMode(5);
connection.connect();
/*
* 注意,下面的getOutputStream函数工作方式于在httpRequestPost()里面的不同
* 在httpRequestPost()里面该函数仍在准备http request,没有向服务器发送任何数据
* 而在这里由于设置了ChunkedStreamingMode,getOutputStream函数会根据connect之前的配置
* 生成http request头,先发送到服务器。
*/
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
String content = "name=" + URLEncoder.encode("发送到服务器 " +
" " +
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "utf-8");
out.writeBytes(content);

out.flush();
out.close(); //到此时服务器已经收到了完整的http request了,而在httpRequestPost()函数里,要等到下一句getInputStream()服务器才能收到http请求。
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));

out.flush();
out.close();
String line;
System.out.println("==========post request开始==========");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
System.out.println("==========post request结束==========");
reader.close();
connection.disconnect();
}
}



HttpURLConnection.connect函数,实际上只是建立了一个与服务器的tcp连接,并没有实际发送http请求。无论是post还是get,http请求实际上直到HttpURLConnection.getInputStream()这个函数里

面才正式发送出去。
在httpRequestPost() 中,顺序很重要,对connection对象的一切配置(那一堆set函数)都必须要在connect()函数执行之前完成。而对 outputStream的写操作,又必须要在inputStream的

读操作之前。这些顺序实际上是由http请求的格式决定的。
http 请求实际上由两部分组成,一个是http头,所有关于此次http请求的配置都在http头里面定义,一个是正文content,在connect()函数里面,会根据HttpURLConnection对象的配置值生

成http头,因此在调用connect函数之前,就必须把所有的配置准备好。
紧接着http头的是http请求的正文,正文的内容通过outputStream写入,实际上outputStream不是一个网络流,充其量是个字符串流,往里面写入的东西不会立即发送到网络,而是在流关闭

后,根据输入的内容生成http正文。
至此,http请求的东西已经准备就绪。在getInputStream()函数调用的时候,就会把准备好的http请求正式发送到服务器了,然后返回一个输入流,用于读取服务器对于此次http请求的返回

信息。由于http请求在getInputStream的时候已经发送出去了(包括http头和正文),因此在getInputStream()函数之后对connection对象进行设置(对http头的信息进行修改)或者写入 outputStream

(对正文进行修改)都是没有意义的了,执行这些操作会导致异常的发生
post请求的OutputStream实际上不是网络流,而是写入内存,在getInputStream中才真正把写道流里面的内容作为正文与根据之前的配置生成的http request头合并成真正的http request,

并在此时才真正向服务器发送。
HttpURLConnection.setChunkedStreamingMode 函数可以改变这个模式,设置了ChunkedStreamingMode后,不再等待OutputStream关闭后生成完整的http request一次过发送,而是先发送

http request头,正文内容则是网路流的方式实时传送到服务器。实际上是不告诉服务器http正文的长度,这种模式适用于向服务器传送较大的或者是不容易获取长度的数据,如文件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值