我尝试从网址http://google.com
和其他网址下载HTML内容。
我试过了:
URL url = new URL("http://google.com/");
urlConnection = (HttpURLConnection) url.openConnection();
虽然我已经尝试InputStream
,但它的效果并不相同。
try {
URL url = new URL("http://google.com/");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
System.out.print(urlConnection.getResponseCode());
}
catch (Exception e)
{
System.out.print("Error: "+e.getMessage());
}
finally {
urlConnection.disconnect();
}
最后,我看到catch
和e.getMessage
是null
。如果我进行调试,urlConnection.getResponseCode()
将返回-1
。
答案 0 :(得分:0)
您将DoOutput设置为不进行输出的位置。您没有在输入的位置设置DoInput。删除分块流模式。