码迷,mamicode.com
首页 > 编程语言 > 详细

java 发送带cookie的http请求

时间:2017-12-07 16:02:29      阅读:462      评论:0      收藏:0      [点我收藏+]

标签:cookie   java   

try{     String path = "https://www.AA.com/AA";         URL url = new URL(path);         HttpURLConnection con = (HttpURLConnection) url.openConnection();         con.setRequestMethod("GET");         /*con.setRequestProperty("Content-Type",                 "application/x-www-form-urlencoded");         con.setRequestProperty("Content-Length",                 String.valueOf(path.length()));*/         con.setRequestProperty(                 "User-Agent",                 "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36");         con.setRequestProperty("Cookie", "AAId=12345678");         con.connect();         if (con.getResponseCode() == 200) {                      BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));             String line;             StringBuilder sb = new StringBuilder();             while ((line = br.readLine()) != null) {// 循环读取流                 sb.append(line);             }             br.close();// 关闭流             System.out.println(sb);         }else{             System.out.println(con.getResponseCode());         }         con.disconnect();// 断开连接 }catch (Exception e){     System.out.println(e); }


java 发送带cookie的http请求

标签:cookie   java   

原文地址:http://blog.51cto.com/9917353/2048374

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!