public String getCookie() {
HttpURLConnection connection = null;
try {
URL url = new URL("https://**************login");
connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(true);
connection.setRequestMethod("POST");
connection.setInstanceFollowRedirects(true);
connection.setConnectTimeout(3000);
connection.setDoInput(true);
connection.setDoOutput(true);
PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
printWriter.write("{'userName':'******','password':'*********'}");
printWriter.flush();
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
byte[] arr = new byte[1024];
while((len=bis.read(arr))!= -1){
bos.write(arr,0,len);
bos.flush();
}
String cookiesHeader = connection.getHeaderField("Set-Cookie");
bos.close();
return cookiesHeader;
} catch (Exception e) {
e.printStackTrace();
}finally{
connection.disconnect();
}
return null;
}
获取接口请求之后的cookie*(爬虫)
最新推荐文章于 2023-05-12 21:25:49 发布