如下所示: String urlPath = "你的请求链接";String cookie = "要发送的cookie";URL url = new URL(urlPath);URLConnection conn = url.openConnection();conn.setRequestProperty("Cookie", cookie);conn.setDoInput(true);BufferedReader b
如下所示:
String urlPath = "你的请求链接"; String cookie = "要发送的cookie"; URL url = new URL(urlPath); URLConnection conn = url.openConnection(); conn.setRequestProperty("Cookie", cookie); conn.setDoInput(true); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while ((line = br.readLine()) != null) { sb.append(line); } System.out.println("请求响应结果:"+sb);
conn.setRequestProperty(“Cookie”, cookie) 这句在设置cookie,以上以get请求为例。
以上这篇在java中http请求带cookie的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自由互联。