leen_wei 2019-04-11 17:27 采纳率: 100%
浏览 821
已采纳

怎么使用HttpURLConnection的post方式获取某个网页数据

我使用如下方式想要获取某个网页的数据:

            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>

  • 写回答

1条回答

  • threenewbee 2019-04-11 20:09
    关注

    301 Moved Permanently
    说明你的网页有一个重定向跳转,可能是提交成功了跳转,也可能是失败了跳转。无论如何,你可以继续获得新的地址,再获取网页,看看返回什么。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题