为了账号安全,请及时绑定邮箱和手机立即绑定

使用HttpURLConnection进行get请求,返回状态码403,网上设置代理的方式均已试过无效,求解

使用HttpURLConnection进行get请求,返回状态码403,网上设置代理的方式均已试过无效,求解

白猪掌柜的 2019-03-01 10:47:44
public static String sendGet(String url){ String message = null; try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); //必须是get方式请求 http.setRequestMethod("GET"); http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setRequestProperty("Cookie", "dsdsd"); http.setRequestProperty("Referer", url); http.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA)"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒 http.connect(); InputStream is =http.getInputStream(); int size =is.available(); byte[] jsonBytes =new byte[size]; is.read(jsonBytes); message=new String(jsonBytes,"UTF-8"); System.out.println("getMessage = "+message); } catch (Exception e) { e.printStackTrace(); } return message; }
查看完整描述

3 回答

?
长风秋雁

TA贡献1757条经验 获得超7个赞

没下文了啊,

查看完整回答
反对 回复 2019-03-01
  • 3 回答
  • 0 关注
  • 1044 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信