java 发送带cookie的http请求
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 发送带cookie的http请求相关的知识,希望对你有一定的参考价值。
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请求的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 上使用 cookie 发出 http 请求?