为何setRequestMethod(“GET”)不生效
来源:互联网 发布:淘宝客应该怎么做 编辑:程序博客网 时间:2024/04/28 05:31
关键是你运行了这一句:httpurlconnection.setDoOutput(true); 就把请求改变为post请求了,请求头的输出并不需要通过httpurlconnection的输出流,只要设置就行,httpurlconnection会在你需要得到服务器响应的时候使用socket连接web服务器,将请求头输出到web服务器,所以这样写就可以了:
public static void doPost() {
try {
url = new URL("http://www.XXXX.com/blogs/list");
httpurlconnection = (HttpURLConnection) url.openConnection();
//httpurlconnection.setDoOutput(true); 这一句不要
//httpurlconnection.setRequestMethod("GET"); 这一句不要,缺省就是get
// httpurlconnection.getOutputStream().write(getFileString().getBytes());
httpurlconnection.setRequestProperty("Cookie", "");
httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
httpurlconnection.setRequestProperty("Referer", "http://extjs2.javaeye.com/");
httpurlconnection.setRequestProperty("Host", "www.javaeye.com");
//httpurlconnection.getOutputStream().flush(); 这一句不要
//httpurlconnection.getOutputStream().close(); 这一句不要
int code = httpurlconnection.getResponseCode();
System.out.println(httpurlconnection.getContentEncoding());
System.out.println(httpurlconnection.getContent());
//System.out.println(httpurlconnection.getOutputStream());
System.out.println("code " + code);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
} setRequestMethod
public static void doPost() {
try {
url = new URL("http://www.XXXX.com/blogs/list");
httpurlconnection = (HttpURLConnection) url.openConnection();
//httpurlconnection.setDoOutput(true); 这一句不要
//httpurlconnection.setRequestMethod("GET"); 这一句不要,缺省就是get
// httpurlconnection.getOutputStream().write(getFileString().getBytes());
httpurlconnection.setRequestProperty("Cookie", "");
httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
httpurlconnection.setRequestProperty("Referer", "http://extjs2.javaeye.com/");
httpurlconnection.setRequestProperty("Host", "www.javaeye.com");
//httpurlconnection.getOutputStream().flush(); 这一句不要
//httpurlconnection.getOutputStream().close(); 这一句不要
int code = httpurlconnection.getResponseCode();
System.out.println(httpurlconnection.getContentEncoding());
System.out.println(httpurlconnection.getContent());
//System.out.println(httpurlconnection.getOutputStream());
System.out.println("code " + code);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
} setRequestMethod
- 为何setRequestMethod(“GET”)不生效
- 为何setRequestMethod(“GET”)不生效
- 为何用setRequestMethod(“GET”)下载不到资源
- OpenCV竖屏问题---为何setDisplayOrientation(90)不生效?
- 修改Hosts为何不生效,是DNS缓存?
- 为何get_argument不区分POST与GET?
- Libvirt为ovs port配置的ingress qos规则为何不生效呢? (by quqi99)
- shiro注解不生效(表象)
- 【source命令在shell脚本中执行为何不生效】source not working from within shell script
- 为什么flush不生效?
- iptables不生效解决办法
- dojo样式不生效
- EL不生效 显示${}
- inotify不生效问题
- php ini_set不生效
- HINT 不生效?
- 配置urlrewrite不生效
- ObjectAnimator.ofFloat不生效
- SQL server2005 restore数据报错
- poj 1125(Stockbroker Grapevine)Dijkstra算法
- Subversion只外的另一款版本控制适用于 国外服务器
- 架构师给程序员的一封信
- sql server 2005/2008 加密存储过程解密
- 为何setRequestMethod(“GET”)不生效
- 软件架构师应该知道的97件事
- D3DMULTISAMPLE_TYPE是什么意思,请问怎么用的
- JAVA Clone机制
- 关于Linux静态库和动态库的分析
- object-c 内存管理
- Hibernate查询缓存全面分析
- 今天又搞到个libDTL.so is not an ELF file - it has the wrong magic bytes at the start.
- unix环境高级编程编译方法 -apue最简单编译方法(第二版)