HttpURLConnection模拟 POST 动作出错

gc2001_xf 2012-01-05 11:40:15
我有下面一段代码 ,当把注释部分去掉后, 会出错,请高人帮我看一下,不胜感激

不执行注释段代码,程序不会出错,URL也是可以访问的


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;
public class HelloWorld {
public static void main(String[] args) throws IOException {
HttpURLConnection con = null;


String username="authenticityToken=9234f0328ba92059a816562d05988a1a8d5fc6fd";
username=URLEncoder.encode(username, "utf-8");
URL url = new URL("http://10.224.104.178/admin/validateT");
con =(HttpURLConnection) url.openConnection();

/*
con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
con.setRequestProperty("Content-Length", "" +Integer.toString(username.getBytes().length));
con.setRequestProperty("Content-Language", "en-US");

con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );

con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");

con.getOutputStream().write(username.getBytes());
con.getOutputStream().flush();
con.getOutputStream().close();
*/

BufferedReader br = null;
String charset = charset(con.getContentEncoding(), con.getContentType());

try {
br = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
for(String str = null; (str = br.readLine()) != null; ) {
System.out.println(str);
}
} finally {
if(br != null) {
br.close();
}
}
}
private static String charset(String encoding, String contentType) {
if(encoding != null && Charset.isSupported(encoding)) {
return encoding;
}
if(contentType == null) {
return Charset.defaultCharset().name();
}
int index = contentType.indexOf("charset=");
if(index < 0) {
return Charset.defaultCharset().name();
}
String charset = contentType.substring(index + 8);
if(!Charset.isSupported(charset)) {
return Charset.defaultCharset().name();
}
System.out.println("charset="+charset);
return charset;
}
}



错误信息



Exception in thread "main" java.io.FileNotFoundException: http://10.224.104.178/admin/validateT
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at HelloWorld.main(HelloWorld.java:39)
Caused by: java.io.FileNotFoundException: http://10.224.104.178/admin/validateT
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(Unknown Source)
at java.net.URLConnection.getContentEncoding(Unknown Source)
at HelloWorld.main(HelloWorld.java:36)

...全文
892 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
MiceRice 2012-01-06
  • 打赏
  • 举报
回复
没有环境测试你的代码,不过建议楼主可以考虑用开源的HttpClient,封装的比较好,且支持Cookie。这样楼主你就可以不需要自己去模拟构造一个POST请求了,大大降低代码复杂度。
gc2001_xf 2012-01-06
  • 打赏
  • 举报
回复
java httpurlconnection 所做的POST是否支持SESSION, 是不是这钟执行方式和浏览器有区别导致的
xinlan1022 2012-01-06
  • 打赏
  • 举报
回复
应该把设置属性放在连接之前吧
fengyungl 2012-01-06
  • 打赏
  • 举报
回复
看报错信息是找不到资源
java.io.FileNotFoundException: http://10.224.104.178/admin/validateT
kouyiSC 2012-01-06
  • 打赏
  • 举报
回复
引用
1. 当把注释部分去掉后, 会出错,请高人帮我看一下,不胜感激
2. 不执行注释段代码,程序不会出错,URL也是可以访问的


好像表达出来的意思是:
1. 注释去掉,会出错。。
2. 注释去掉,不会出错。。

Guo 2012-01-06
  • 打赏
  • 举报
回复
http://blog.csdn.net/guobing965816/article/details/7108730

这里有这方面的资料!!!
Steve 2012-01-06
  • 打赏
  • 举报
回复
http://10.224.104.178/admin/validateT
不支持POST吧,用GET试试。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧