Taneeyo 2020-06-07 16:03 采纳率: 0%
浏览 282

请问在爬取网站时遇到cookie中同名JSESSIONID对应多个Value,但是客户端携带的cookie只有一个怎么解决呢?

请问在爬取网站时遇到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();  }
    }

}

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2020-09-01 11:15
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!