Http学习之使用HttpURLConnection发送post请求深入
来源:互联网 发布:linux 开机执行命令 编辑:程序博客网 时间:2024/05/20 03:45
接上节 Http学习之使用HttpURLConnection发送post和get请求
本节深入学习post请求。
上节说道,post请求的OutputStream实际上不是网络流,而是写入内存,在getInputStream中才真正把写道流里面的内容作为正文与根据之前的配置生成的http request头合并成真正的http request,并在此时才真正向服务器发送。
HttpURLConnection.setChunkedStreamingMode函数可以改变这个模式,设置了ChunkedStreamingMode后,不再等待OutputStream关闭后生成完整的http request一次过发送,而是先发送http request头,正文内容则是网路流的方式实时传送到服务器。实际上是不告诉服务器http正文的长度,这种模式适用于向服务器传送较大的或者是不容易获取长度的数据,如文件。下面以一段代码讲解一下,请与Http学习之使用HttpURLConnection发送post和get请求中的readContentFromPost()函数作对比:
本节深入学习post请求。
上节说道,post请求的OutputStream实际上不是网络流,而是写入内存,在getInputStream中才真正把写道流里面的内容作为正文与根据之前的配置生成的http request头合并成真正的http request,并在此时才真正向服务器发送。
HttpURLConnection.setChunkedStreamingMode函数可以改变这个模式,设置了ChunkedStreamingMode后,不再等待OutputStream关闭后生成完整的http request一次过发送,而是先发送http request头,正文内容则是网路流的方式实时传送到服务器。实际上是不告诉服务器http正文的长度,这种模式适用于向服务器传送较大的或者是不容易获取长度的数据,如文件。下面以一段代码讲解一下,请与Http学习之使用HttpURLConnection发送post和get请求中的readContentFromPost()函数作对比:
public static void readContentFromChunkedPost()throws IOException...{
URL postUrl= new URL(POST_URL);
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");
/**//*
* 与readContentFromPost()最大的不同,设置了块大小为5字节
*/
connection.setChunkedStreamingMode(5);
connection.connect();
/**//*
* 注意,下面的getOutputStream函数工作方式于在readContentFromPost()里面的不同
* 在readContentFromPost()里面该函数仍在准备http request,没有向服务器发送任何数据
* 而在这里由于设置了ChunkedStreamingMode,getOutputStream函数会根据connect之前的配置
* 生成http request头,先发送到服务器。
*/
DataOutputStream out= new DataOutputStream(connection
.getOutputStream());
String content= "firstname="+ URLEncoder.encode("一个大肥人" +
" "+
"asdfasfdasfasdfaasdfasdfasdfdasfs","utf-8");
out.writeBytes(content);
out.flush();
out.close();// 到此时服务器已经收到了完整的http request了,而在readContentFromPost()函数里,要等到下一句服务器才能收到http请求。
BufferedReader reader= new BufferedReader(new InputStreamReader(
connection.getInputStream()));
out.flush();
out.close();// flush and close
String line;
System.out.println("=============================");
System.out.println("Contents of post request");
System.out.println("=============================");
while ((line = reader.readLine())!= null)...{
System.out.println(line);
}
System.out.println("=============================");
System.out.println("Contents of post request ends");
System.out.println("=============================");
reader.close();
connection.disconnect();
}
URL postUrl= new URL(POST_URL);
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");
/**//*
* 与readContentFromPost()最大的不同,设置了块大小为5字节
*/
connection.setChunkedStreamingMode(5);
connection.connect();
/**//*
* 注意,下面的getOutputStream函数工作方式于在readContentFromPost()里面的不同
* 在readContentFromPost()里面该函数仍在准备http request,没有向服务器发送任何数据
* 而在这里由于设置了ChunkedStreamingMode,getOutputStream函数会根据connect之前的配置
* 生成http request头,先发送到服务器。
*/
DataOutputStream out= new DataOutputStream(connection
.getOutputStream());
String content= "firstname="+ URLEncoder.encode("一个大肥人" +
" "+
"asdfasfdasfasdfaasdfasdfasdfdasfs","utf-8");
out.writeBytes(content);
out.flush();
out.close();// 到此时服务器已经收到了完整的http request了,而在readContentFromPost()函数里,要等到下一句服务器才能收到http请求。
BufferedReader reader= new BufferedReader(new InputStreamReader(
connection.getInputStream()));
out.flush();
out.close();// flush and close
String line;
System.out.println("=============================");
System.out.println("Contents of post request");
System.out.println("=============================");
while ((line = reader.readLine())!= null)...{
System.out.println(line);
}
System.out.println("=============================");
System.out.println("Contents of post request ends");
System.out.println("=============================");
reader.close();
connection.disconnect();
}
http://blog.csdn.net/pandazxx/article/details/1660008
- Http学习之使用HttpURLConnection发送post请求深入
- Http学习之使用HttpURLConnection发送post请求深入
- Http学习之使用HttpURLConnection发送post请求深入 .
- Http学习之使用HttpURLConnection发送post请求深入
- Http学习之使用HttpURLConnection发送post请求深入
- Http学习之使用HttpURLConnection发送post请求深入
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求 .
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- Http学习之使用HttpURLConnection发送post和get请求
- 整理Gmail邮箱的大邮件
- 基于Linux操作系统的底层驱动技术
- Edge-perserving tech
- mini2440跳线开关
- java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE是什么意思
- Http学习之使用HttpURLConnection发送post请求深入
- CGContextRef
- 项目经验——Sql server 数据库的备份和还原____还原数据库提示“介质集有2个介质簇,但只提供了1个。必须提供所有成员”
- WritePrivateProfileString 设置为程序当前路径的方法
- 引入CSS样式的几种方式
- 运行shell脚本permission deny
- 二次催稿
- spring+hibernate 实体类注解问题
- 数据结构学习笔记-平衡二叉树实现