3

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

使用的代码在这里

     尝试{
        响应 res = Jsoup.connect(LOGIN_URL)
    .data(“用户名”,“等等”,“密码”,“等等”)

    .method(方法.POST)
  .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0")
                .header("接受", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
                。执行();
        System.out.println(res.body());
        System.out.println("代码" +res.statusCode());

        }
        捕获(异常 e){
            System.out.println(e.getMessage());
        }

还有这个

文档 doc = Jsoup.connect(LOGIN_URL)
  .data(“用户名”,“废话”)
  .data(“密码”,“废话”)
  .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0")
        .header("内容类型", "application/x-www-form-urlencoded")
         .method(方法.POST)
  .timeout(3000)
  。邮政();
            

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

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

如何通过 HTTPS 发布

编辑:

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

4

2 回答 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();

我已经使用此代码来获取新页面的标题。

于 2013-03-06T06:13:16.437 回答
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();
}

如果需要进一步的帮助,请发表评论。

于 2017-12-08T18:20:58.723 回答