Jsoup返回请求的同一页面

时间:2019-01-28 19:39:11

标签: java jsoup html-parsing

我试图在验证码验证后解析页面,但是无法从Jsoup得到正确的响应。

这里是我的代码:

首先,a请求获取params页面和Cookies:

Connection.Response response = Jsoup.connect("https://nfce.set.rn.gov.br/portalDFE/NFCe/")
            .method(Connection.Method.GET)
            .execute();



    Map<String,String> cookies = response.cookies();
    Document doc = response.parse();
    String param1 = doc.getElementById("__LASTFOCUS").attr("value");
    String param2 = doc.getElementById("__EVENTTARGET").attr("value");
    String param3 = doc.getElementById("__EVENTARGUMENT").attr("value");
    String param4 = doc.getElementById("__VIEWSTATE").attr("value");
    String param5 = doc.getElementById("__VIEWSTATEGENERATOR").attr("value");
    String param6 = doc.getElementById("__EVENTVALIDATION").attr("value");

其次,第二次请求插入参数和之前请求中获得的cookie,以将验证码图像发送给用户。我正在设法主持会议。

Connection con = Jsoup.connect("https://nfce.set.rn.gov.br/portalDFE/JpegImage.aspx")

            .method(Connection.Method.GET);
    con.cookie("ASP.NET_SessionId",cookies.get("ASP.NET_SessionId"));
    con.cookie("ARRAffinity",cookies.get("ARRAffinity"));
    con.data(cookies);
    con.data("__LASTFOCUS",param1);
    con.data("__EVENTTARGET",param2);
    con.data("__EVENTARGUMENT",param3);
    con.data("__VIEWSTATE",param4);
    con.data("__VIEWSTATEGENERATOR",param5);
    con.data("__EVENTVALIDATION",param6);
    con.ignoreContentType(true);
byte[] bytes = con.execute().bodyAsBytes();
    ImageIcon imagem = new ImageIcon();
    InputStream is = new ByteArrayInputStream(bytes);
    Image image = new ImageIcon(bytes).getImage();
    ImageIcon icon = new ImageIcon(image);
    JFrame frame = new JFrame("Captcha");
    JLabel label = new JLabel();

    frame.getContentPane().add(label, BorderLayout.CENTER);



    String captcha = (String) JOptionPane.showInputDialog(frame,label,"Test"    ,JOptionPane.INFORMATION_MESSAGE,icon,null,null);

最后,我遇到麻烦了。我正在尝试请求获取要解析的页面,但是由于某种原因,我仍无法理解,因此我正在使用不同的URL获取同一页面。

Connection con2 = Jsoup.connect("https://nfce.set.rn.gov.br/portalDFE/NFCe/DadosNFCe.aspx").method(Connection.Method.POST);
    con2.data("tipoConsulta","resumida");
    con2.data("ctl03$txt_chave_acesso","24190109374422000363650020001921751004777144");
    con2.data("txt_cod_antirobo",captcha);
    con2.cookie("ASP.NET_SessionId",cookies.get("ASP.NET_SessionId"));
    con2.cookie("ARRAffinity",cookies.get("ARRAffinity"));
    con2.data(cookies);
    con2.data("__LASTFOCUS",param1);
    con2.data("__EVENTTARGET",param2);
    con2.data("__EVENTARGUMENT",param3);
    con2.data("__VIEWSTATE",param4);
    con2.data("__VIEWSTATEGENERATOR",param5);
    con2.data("__EVENTVALIDATION",param6);
    con2.ignoreContentType(true);


    con2.header("Connection","keep-alive");
    con2.header("Content-Type","application/x-www-form-urlencoded");
    con2.header("Accept-Encoding","gzip, deflate, br");
    con2.header("Accept-Language","pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7");
    con2.header("Cache-Control","no-cache");
    con2.header("Connection","keep-alive");
    con2.header("Host","nfce.set.rn.gov.br");
    con2.header("Pragma","no-cache");
    con2.header("Upgrade-Insecure-Requests","1");
    con2.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");

在Chorme,它可以工作,但不能通过Jsoup。 这是请求之前和之后页面的外观:

Before Resquest on Browser

After Request on Browser

感谢您的帮助。

0 个答案:

没有答案