我有下面一段代码 ,当把注释部分去掉后, 会出错,请高人帮我看一下,不胜感激不执行注释段代码,程序不会出错,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)

解决方案 »

  1.   

    java httpurlconnection 所做的POST是否支持SESSION, 是不是这钟执行方式和浏览器有区别导致的
      

  2.   

    没有环境测试你的代码,不过建议楼主可以考虑用开源的HttpClient,封装的比较好,且支持Cookie。这样楼主你就可以不需要自己去模拟构造一个POST请求了,大大降低代码复杂度。
      

  3.   

    http://10.224.104.178/admin/validateT
    不支持POST吧,用GET试试。
      

  4.   

    http://blog.csdn.net/guobing965816/article/details/7108730这里有这方面的资料!!!
      

  5.   

    看报错信息是找不到资源
    java.io.FileNotFoundException: http://10.224.104.178/admin/validateT