如何设置Cookie的Http Get方法使用Java [英] How to set Cookies at Http Get method using Java

查看:1466
本文介绍了如何设置Cookie的Http Get方法使用Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Cookie进行手动GET,以便下载和解析网页。我需要提取安全令牌,以便在论坛发帖。我已经完成登录,已经读取响应并提取了Cookie(3对(名称,值))。然后我写了包含这样的cookie的字符串:

I want to do a manual GET with cookies in order to download and parse a web page. I need to extract the security token, in order to make a post at the forum. I have completed the login, have read the response and extracted the cookies (3 pairs of (name,value) ). I then wrote the String containing the cookies like this:

 CookieString="name1=value1; name2=value2; name3=value3"

然后我执行以下操作:

HttpURLConnection connection
connection = (HttpURLConnection)(new URL(Link).openConnection());
connection.setRequestMethod("GET");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cookie", CookieString );
connection.connect();

然后我读了页面,但它显示我没有登录论坛。我做错了什么?

I then read the page but it shows that I am not logged at the forum. What am I doing wrong?

编辑:我知道如果我想发帖,我必须提取安全令牌。我的思路是,为了提取它,我需要获取这个特定的页面。但是对于安全令牌作为一个隐藏字段我必须在线,因此我需要cookie。但是当我获取页面,我设置了如上所述的cookie,我得到的页面作为一个客人,它显示我不在线,安全令牌的价值是客人,这对我没有用。我会检查你给我的链接,希望能找到一个解决方案。

edit: I know that I must extract the security token if I want to make a post. My train of thought was that in order to extract it, I need to GET this particular page. But for the security token to be as a hidden field I must be online, thus I needed the cookies. But when I GET the page and I set the cookies as mentioned above i get the page as a guest, it shows that I am not online and the value of security token is guest which is not useful for me. I will check the link you gave me and hopefully will find a solution.

推荐答案

从响应的 Set-Cookie 头。要在后续请求中将其发送回来,您应该使用 URLConnection#addRequestProperty()

To be sure, you should be gathering the cookies from the response's Set-Cookie headers. To send them back in the subsequent requests, you should set them one by one using URLConnection#addRequestProperty().

基本上:

// ...

// Grab Set-Cookie headers:
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");

// ...

// Send them back in subsequent requests:
for (String cookie : cookies) {
    connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}

// ...

c $ c> split(;,2)是摆脱与服务器端无关的cookie属性,如 expires path 等。

The split(";", 2) is there to get rid of cookie attributes which are irrelevant for the server side like expires, path, etc.

为了更方便的HTTP客户端,我建议看看 Apache HttpComponents客户端

For a more convenienced HTTP client I'd suggest to have a look at Apache HttpComponents Client. It can handle all the cookie stuff more transparently.

  • How to use URLConnection?

更新不是cookie的问题。错误的请求令牌意味着服务器具有CSRF / bot防御内置(以防止像您一样的人)。您需要从具有表单的请求页面中提取令牌作为隐藏输入字段,并将其作为请求参数重新发送。 Jsoup 可能有助于提取所有(隐藏)输入字段。不要忘记传递按钮的名称 - 值对,你想要按编程。

Update: as per the comments, this is not a cookie problem. A wrong request token means that the server has CSRF/bot prevention builtin (to prevent people like you). You need to extract the token as a hidden input field from the requested page with the form and resend it as a request parameter. Jsoup may be useful to extract all (hidden) input fields. Don't forget to pass the name-value pair of the button as well which you'd like to "press" programmatically. Also see the abovementioned link for more hints.

在未来,你应该真的更清楚你检索的确切错误,而不是在野外猜出来的东西。复制确切的错误消息等。

In the future, you should really be more clear about the exact error you retrieve and not guess something in the wild. Copypaste the exact error message and so on.

这篇关于如何设置Cookie的Http Get方法使用Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆