我尝试做以下(在Java中):的Java .getInputStream()的openConnection()HTTP响应代码中的错误
- 连接到一些代理服务器& HTTP_URL一些
但我有像一些错误: java.net.ConnectException: Connection timed out: connect...
或与之相关的HTTP响应代码错误:302, 400, FileNotFound, file server error
等
在我做的一些更改中,我甚至得到了200
的代码。 (当我只用openConnection()
=>(无代理IP地址) 这是我的最佳运行跟踪
我有所有类:。在错误味精(来源不明),从IDE Eclipse的月神控制台。
一些错误进来形式的/或:.getInputStream()
方法,我不知道是否有关于setDoInput()
,setDoOutput
的编码,或什么:?
一些机构可以帮我
这是我的代码:
url = new URL(http_url);
HttpURLConnection conn;
try {
conn = (HttpURLConnection)url.openConnection(proxy);
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent", USERAGENT);
conn.setUseCaches(false);
conn.setRequestProperty("Accept", "*/*");
conn.addRequestProperty("Referer", "http://www.google.com/");
conn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
conn.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
conn.setDoInput(true);
System.out.println("response msg " + conn.getResponseMessage() + " CODE");
System.out.println("errorStream msg " + conn.getErrorStream());
System.out.println("inputStream msg " + conn.getInputStream());
String header_date = conn.getHeaderField("Date");
System.out.println(" date es: " + header_date);
String line = null;
StringBuffer tmp = new StringBuffer();
System.out.println("the code is :" + conn.getResponseCode());
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = in.readLine()) != null) {
tmp.append(line);
}
System.out.println("value line is: " + line +"& date is: " + header_date);
Scrape(String.valueOf(tmp)); // temp.toString()
in.close();
in = null;
url = null;
conn.disconnect();
conn = null;
} else {
System.out.println("something bad happened code <>200, debug from your server");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
你应该明确地编辑您的问题,所以它变得更容易阅读剥离下来。还删除你的代码,以显示你有一个问题。我试图理解你的问题,但没有这样做... – Jens
谢谢Jens我知道文本密集,但问题是openConnection(代理)如果你不使用代理代码工作正常,如果您使用:System.setProperty(“ http.proxyHost“,Host); \t System.setProperty(“http.proxyPort”,Port);它也适用于什么问题?它是一个需要扩展/覆盖的抽象类吗? –
我已经阅读了关于openConnection(代理)的StackOverflow问题,并看到有很多人在这个相同的问题堆叠。关于openConnection()方法的代理问题,我没有注意到Java中的一个主要错误。 –