Java - 为什么 HttpClient 不发送我的 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15876598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:09:36  来源:igfitidea点击:

Java - Why HttpClient doesn't send my cookies?

javacookieshttpclient

提问by user2173353

I try to send cookies with a form post using the Apache HttpClient and, for some reason, the server gets the request but not the cookies. Here is my code:

我尝试使用 Apache HttpClient 发送带有表单帖子的 cookie,由于某种原因,服务器收到请求,但没有收到 cookie。这是我的代码:

            DefaultHttpClient client = new DefaultHttpClient();

            // Set the cookies...
            {
                String Domain = MyGetParameter("Domain");
                BasicCookieStore cookieStore = new BasicCookieStore(); 
                String[] strs = GetParameterSplitted("PostCookies");
                int size = strs.length;
                for (int i=0; i<size-1; i+=2)
                {
                    //JOptionPane.showMessageDialog(null, strs[i]+" = "+FromBase64(strs[i+1], "UTF-8"));
                    BasicClientCookie cookie = new BasicClientCookie(strs[i], FromBase64(strs[i+1], "UTF-8"));
                    cookie.setDomain(Domain);
                    cookie.setPath("/");
                    //cookie.setSecure(true);
                    cookieStore.addCookie(cookie);
                }
                client.setCookieStore(cookieStore);
            }

            HttpPost post = new HttpPost(url.toURI());
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(10);
            // Set the form POST parameters...
            {
                String[] strs = GetParameterSplitted("PostParams");
                int size = strs.length;
                for(int i=0; i<size-1; i+=2)
                {
                    String name = strs[i].trim();
                    String value = FromBase64(strs[i+1].trim(), "UTF-8");//, "UTF-8"

                    nameValuePairs.add(new BasicNameValuePair(name, value));
                }
            }
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            post.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            post.getParams().setParameter(ClientPNames.COOKIE_POLICY, org.apache.http.client.params.CookiePolicy.BROWSER_COMPATIBILITY);

            HttpResponse response = client.execute(post);
            int StatusCode = response.getStatusLine().getStatusCode();

The site uses HTTP (not HTTPS), I make sure the domain name is set correctly to the cookies (http://mysite) and the cookies seem to be set correctly when the above code executes.

该站点使用 HTTP(而不是 HTTPS),我确保将域名正确设置为 cookie ( http://mysite) 并且在执行上述代码时 cookie 似乎设置正确。

Does anyone have any idea why it's failing to pass them to the server? I have seen other similar questions on this site but nothing seemed to help.

有谁知道为什么它无法将它们传递给服务器?我在这个网站上看到了其他类似的问题,但似乎没有任何帮助。

回答by mr.boyfox

You look closely, if the date of your cookie expired httptClient not send this cookie, on this that you should put the cookies date.

你仔细看,如果你的cookie过期日期httptClient没有发送这个cookie,在这个你应该把cookie日期放上去。

And in domain name will be no "http://", only simply domain name.

并且在域名中将没有“http://”,只有简单的域名。

For Example:(http://www.gmail.com=> like this to write setDomain("www.gmail.com"))

例如:( http://www.gmail.com=> 这样写setDomain("www.gmail.com" ))

This example i add 100 day to current day and set cookie. Example send post data via HttpClient with cookie:

这个例子我将 100 天添加到当天并设置 cookie。示例通过带有 cookie 的 HttpClient 发送帖子数据:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 100);
Date date = calendar.getTime();

DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.setCookieStore(new BasicCookieStore());
BasicClientCookie cookie = new BasicClientCookie(YourCookieName, YourCookieValue);
cookie.setDomain(YourDomain);
cookie.setExpiryDate(date);
cookie.setPath("/");
httpClient.getCookieStore().addCookie(cookie);

....

httpClient.execute(yourHttpUriRequest);