a1670131049 2020-10-13 12:07 采纳率: 50%
浏览 203
已采纳

java http get方式请求失败

GET https://api.huoyisp.com/user/login?phone=18839654919&password=a41172419&type=uap HTTP/1.1
user-agent: PAFM00(Android/10) (uni.huoyisp.com/1.0.9) Weex/0.26.0 1080x2340
Host: api.huoyisp.com
Connection: Keep-Alive
Accept-Encoding: gzip
Cookie: PHPSESSID=hgv2gtplms1unihc72i727hfia

这个是请求体,也是没有响应

String urls="https://api.huoyisp.com/user/login?phone="+p.getName()+"&password="+p.getPwd()+"&type=uap";
        try {
            URL url=new URL(urls);
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();

            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");

            conn.setRequestProperty("user-agent", "PAFM00(Android/10) (uni.huoyisp.com/1.0.9) Weex/0.26.0 1080x2340");
            conn.setRequestProperty("Host", "api.huoyisp.com");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Accept-Encoding", "gzip");
            conn.setRequestProperty("Cookie", "PHPSESSID=hgv2gtplms1unihc72i727hfia");

            int code=conn.getResponseCode();
            } catch (IOException e) {
            }
  • 写回答

1条回答 默认 最新

  • _YuLF 2020-10-13 13:38
    关注
     public static void main(String[] args) {
            try {
                URL url=new URL("http://www.huli667.com:81/sms/api/login?username=api-ArIGbJDn&password=a411724");
                HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
                int code=conn.getResponseCode();
                if(code==200){
                    String res = "",line="";
                    InputStream in=conn.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
                    while( (line = bufferedReader.readLine()) != null )
                    {
                        res += line;
                    }
                    System.out.println(res);
                    bufferedReader.close();
                    in.close();
                }
                conn.disconnect();
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    
    

    发现用HTTP测试正常,但是用你的代码就不行,然后我加了个请求头参数,
    图片说明

    应该是你服务端上面的判断

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

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器