4

我尝试使用 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();

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

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

4

1 回答 1

17

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

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

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

这个例子我将 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);
于 2013-04-08T10:54:56.250 回答