首页 文章

jsoup发布Java

提问于
浏览
3

我正在努力让java通过HTTPS提交POST请求

使用的代码在这里

try{
        Response res = Jsoup.connect(LOGIN_URL)
    .data("username", "blah", "password", "blah")

    .method(Method.POST)
  .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0")
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                .execute();
        System.out.println(res.body());
        System.out.println("Code " +res.statusCode());

        }
        catch (Exception e){
            System.out.println(e.getMessage()); 
        }

还有这个

Document doc = Jsoup.connect(LOGIN_URL)
  .data("username", "blah")
  .data("password", "blah")
  .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0")
        .header("Content-type", "application/x-www-form-urlencoded")
         .method(Method.POST)
  .timeout(3000)
  .post();

LOGIN_URL = https://xxx.com/Login?val=login

当通过HTTP使用它似乎工作,HTTPS它没有,但不会抛出任何异常

如何通过HTTPS进行POST

编辑:

似乎当服务器通过HTTPS获取POST时会涉及302重定向(这不会通过http发生)我如何使用jsoup将随302发送的cookie存储到下一页?

2 回答

  • 2

    这是我的代码:

    URL form = new URL(Your_url);
    connection1 = (HttpURLConnection)form.openConnection();
    connection1.setRequestProperty("Cookie", your_cookie);
    
    connection1.setReadTimeout(10000);
    StringBuilder whole = new StringBuilder();
    
    BufferedReader in = new BufferedReader(
            new InputStreamReader(new BufferedInputStream(connection1.getInputStream())));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
         whole.append(inputLine);
         in.close();
    Document doc = Jsoup.parse(whole.toString());
    String title = doc.title();
    

    我已使用此代码获取新页面的 Headers .

  • 0

    这是你可以尝试的......

    import org.jsoup.Connection;
    
    
    Connection.Response res = null;
        try {
            res = Jsoup
                    .connect("your-first-page-link")
                    .data("username", "blah", "password", "blah")
                    .method(Connection.Method.POST)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    现在保存所有cookie并向您想要的其他页面发出请求 .

    //Saving Cookies
    cookies = res.cookies();
    

    向另一个页面发出请求 .

    try {
        Document doc = Jsoup.connect("your-second-page-link").cookies(cookies).get();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    

    评论是否需要进一步帮助 .

相关问题