java中http请求带cookie

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)