我使用如下方式想要获取某个网页的数据:
URL url = new URL(str);
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setReadTimeout(15000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
传入的url为http://www.baidu.com或者http://www.csdn.net,使用如下方法后去输入流及返回码:
InputStream input = connection.getInputStream();
int code = connection.getResponseCode();
但是执行的结果却是:
code: 301
respose: <html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>openresty</center>
</body>
</html>
301 Moved Permanently
说明你的网页有一个重定向跳转,可能是提交成功了跳转,也可能是失败了跳转。无论如何,你可以继续获得新的地址,再获取网页,看看返回什么。