如何使用Jsoup登录网站并对Java帐户执行操作

时间:2017-02-19 21:03:10

标签: java jsoup

我想制作一个程序,登录网站(如亚马逊),然后将一个项目放入购物车。

ATM我试图登录该帐户。我目前的代码是:

 Connection.Response initial = Jsoup
    .connect("http://www.sneakersnstuff.com/en/authentication/login/en/authentication/login")
    .method(Connection.Method.GET).execute();

Connection.Response login = Jsoup
    .connect("http://www.sneakersnstuff.com/en/authentication/login/en/authentication/login")
    .data("username","MYUSERNAME")
    .data("password","MYPASSWOR")
    .cookies(initial.cookies())
    .method(Method.POST)
    .execute();

然后我收到此错误消息:

Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=http://www.sneakersnstuff.com/en/authentication/login/en/authentication/login
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:598)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:548)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:235)
at sneaker.Sneaker.main(Sneaker.java:98)

我想登录的网站是:http://www.sneakersnstuff.com/en/authentication/login/ 并且登录的表单操作是: http://www.sneakersnstuff.com/en/authentication/login/en/authentication/login

什么是不对的?

1 个答案:

答案 0 :(得分:0)

HTTP 403状态码表示Forbidden。很可能该网站想要拒绝像你这样的程序化访问。大部分时间都是对User-Agent HTTP头字段进行弱检查。当你伪造它时,它可能会起作用。但很明显,您违反了网站所有者拒绝以编程方式访问其网站的意图。