2011-09-18 17 views
19

我使用HttpClient最新版本(4.x)。而現在我正在嘗試做一個GET請求。 我只是發佈一個Get請求。HttpClient警告:Cookie被拒絕:非法域屬性

這是我的代碼;

public class Poster { 

    static boolean routing1 = true, routing2 = true; 
    static int counter1 = 0, counter2 = 0; 
    DefaultHttpClient oHtp = null; 
    HttpGet oHGet = null; 
    HttpResponse oHRes = null; 


    private void test(String fullAddress) throws Exception { 
     oHtp = new DefaultHttpClient(); 
     oHGet = new HttpGet(fullAddress); 

     HttpResponse response = oHtp.execute(oHGet); 
     System.out.print(response.getStatusLine()); 

     HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      entity = new BufferedHttpEntity(entity); 
      // System.out.println(EntityUtils.toString(entity)); 
      System.out.print("\t entity is retrieved... "); 
     } 


     oHtp.getConnectionManager().shutdown(); 
    } 
} 

我只是很好地執行它。 首先是

new Poster().test("http://123.xl.co.id/profile.php"); 

和第二是

new Poster().test("http://goklik.co.id/"); 

雅,只有第二個....我得到這個錯誤信息;

2011年9月18日上午10時11分30秒 org.apache.http.client.protocol.ResponseProcessCookies processCookies 警告:Cookie的拒絕:「[版本:0] [名:CookiePst] [值: 0149 = xwGHF7HYDHLHQ84Isp/eSy9vu + Xq6cT12wxg1A ==] [域名: .mcore.com] [路徑:/] [截止日期:Sun Sep 18 10:38:59 ICT 2011]「。非法 域屬性「mcore.com」。來源域:「goklik.co.id」

我意識到,餅乾這裏所涉及的。但我不明白警告是什麼意思。而且我也不知道如何解決它(Cookie不被拒絕)。 HOpe有一點點清楚我的想法,你們......:D

回答

16

你不能「修復」它。該網站正在嘗試設置一個不允許設置的cookie,並且您正在使用的apache客戶端庫正在告訴你。

它試圖設置cookie爲mcore.com當域goklik.co.id

+0

呵呵??你確定嗎?? OMG。我想,從客戶端(我的方面),我應該重新創建我的cookie之前給另一個GET請求(傳遞的cookie)使用mcore.com,而不是所需的goklik.co.id :( – gumuruh

+0

你是使用客戶端符合正確的標準。查看'org.apache.http.client'的JavaDocs我沒有看到重寫的方法(可用的org.apache.http.client.params.CookiePolicy選項都是標準) –

+0

我有同樣的問題,但我發送請求到同一網站。你是什麼意思「該網站試圖設置一個cookie不允許設置」。爲什麼這是問題?在我的情況下(同樣的網站),我真的沒有辦法解決這個問題嗎? – Jaskey

21

也許爲時已晚,但我有同樣的問題,我發現的東西,幫我解決它,只需設置Cookie Policy到瀏覽器保持兼容:

httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, 
     CookiePolicy.BROWSER_COMPATIBILITY); 

以下是可能的值:

器的cookie策略提供了相應的cookie管理間對於給定類型或版本的cookie,frace 。

默認情況下使用RFC 2109規範。其他支持 規格可以選擇在適當的時候還是默認設置時 所需

提供以下規格:

  • BROWSER_COMPATIBILITY:與普通的cookie管理辦法(即使不是100個%符合標準的兼容)
  • NETSCAPE:網景的cookie草案兼容
  • RFC_2109:RFC2109標準(默認)
  • IGNORE_COOKIES:不automcatically過程餅乾
+0

這對我不起作用。還有什麼我可以做的嗎? –

+6

我有同樣的問題,但因爲我對Cookie不感興趣,所以我設置了CookiePolicy。 IGNORE_COOKIES,這對我來說(我保留了BROWSER_COMPTIBILITY)得到消息)。 –

+0

此消息的含義是什麼以及CookiePolicy.BROWSER_COMPATIBILITY是什麼意思? – Jaskey

10

之前httpclient 4.3,Jonathan Silva的回答很酷。因爲httpclient 4.3

,API似乎發生了很大變化,下面的代碼將工作:

RequestConfig customizedRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build(); 
HttpClientBuilder customizedClientBuilder = HttpClients.custom().setDefaultRequestConfig(customizedRequestConfig); 
CloseableHttpClient client = customizedClientBuilder.build(); // customized client, 
0

我使用的HTTP客戶端4.5.2,這是設置cookie的規範,以方便解決我的問題。如何實例化客戶端的例子:

httpClient = HttpClients.custom() 
       .setDefaultRequestConfig(RequestConfig.custom() 
         // Waiting for a connection from connection manager 
         .setConnectionRequestTimeout(10000) 
         // Waiting for connection to establish 
         .setConnectTimeout(5000) 
         .setExpectContinueEnabled(false) 
         // Waiting for data 
         .setSocketTimeout(5000) 
         .setCookieSpec("easy") 
         .build()) 
       .setMaxConnPerRoute(20) 
       .setMaxConnTotal(100) 
       .build(); 
0

只想提升Eric的答案,因爲它不直接解決了我的情況,但改變CookieSpecs到IGNORE_COOKIES解決我的問題。

RequestConfig customizedRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); 
HttpClientBuilder customizedClientBuilder = 
HttpClients.custom().setDefaultRequestConfig(customizedRequestConfig); 
CloseableHttpClient client = customizedClientBuilder.build(); // customized client, 

因爲在我的HttpClient 4.5版本中CookieSpecs.BROWSER_COMPATIBILITY已經摺舊了。

相關問題