请问在爬取网站时遇到cookie中同名JSESSIONID对应多个Value,但是客户端携带的cookie只有一个怎么解决呢?
感谢各位前辈指导
我的客户端代码如下:
public static void main(String[] args) throws IOException{
downloadImgReturnCookie(verifyImgSrcURL,verifyImgDestURL);
System.out.println(cookies);
//input verifyCode
System.out.println("please input ur code");
verifyCode=new Scanner(System.in).nextLine();
reqData.put("RANDOMCODE",verifyCode);
Connection loginConn= Jsoup.connect(loginHomePage);
loginConn.data(reqData);
loginConn.cookies(cookies);
//try to login
Connection.Response loginResponse=loginConn.execute();
//System.out.println(loginResponse.body());
//TODO 这里可以对主页进行操作
//登录成功
if(loginResponse.statusCode()==200){
System.out.println("login status:"+loginResponse.statusCode()+","+loginResponse.statusMessage());
Connection tableConn=Jsoup.connect(lessionsPage);
tableConn.cookies(cookies);
Connection.Response responseTablePage=null;
responseTablePage=tableConn.execute();
System.out.println("login status:"+responseTablePage.statusCode()+","+responseTablePage.statusMessage());
System.out.println(responseTablePage.body());
}
}
private static void downloadImgReturnCookie(String srcUrl, String dest) throws MalformedURLException, IOException{
//get image using the cookie
HttpURLConnection imgConn = (HttpURLConnection) ( new URL(srcUrl) ).openConnection();
//get the cookie
String cookie = imgConn.getHeaderField("Set-Cookie");
sessionValue = cookie.substring( cookie.indexOf('=') + 1, cookie.indexOf(';') );
cookies.put( sessionName, sessionValue );
//new input from network( 'imgConn' )
try( BufferedInputStream imgInputStream = new BufferedInputStream( imgConn.getInputStream() ) ){
//new output to local file system
try( BufferedOutputStream imgOutputStream = new BufferedOutputStream( new FileOutputStream(dest) ); ){
byte[] buf = new byte[1024];
while( -1 != ( imgInputStream.read(buf) ) )
imgOutputStream.write(buf);
} catch(IOException e) { e.printStackTrace(); }
} catch(IOException e) { e.printStackTrace(); }
}
}