2013-05-16 57 views
-1

我想發送CookieHttpGet Url。從我的服務(由HttpGet調用)收到,用於身份驗證。我使用Url發送它,但每次都會收到錯誤消息。如何使用HttpGet網址發送Cookie?

like:「User must Autuetificated」。

我得到以下錯誤

05-15 10:28:17.623: I/System.out(18393): <root> 
05-15 10:28:17.623: I/System.out(18393): <status>0</status> 
05-15 10:28:17.623: I/System.out(18393): <error>User must be Authentificated</error> 
05-15 10:28:17.623: I/System.out(18393): </root> 
05-15 10:28:17.623: I/System.out(18393): response =: [email protected] 
05-15 10:28:17.623: I/System.out(18393): sts value =: 0 
05-15 10:28:17.623: I/System.out(18393): print customer actived bid api sts values 1 che... 

截圖我的演示與錯誤信息。 enter image description here

人有一個想法來解決這個問題...

+0

你可以添加代碼嗎?旁註:你知道這是*取消*而不是* cencel *對嗎? – Rob

回答

0

服務器會送你一個Set-Cookie頭,你將不得不使用來獲得Cookie和所有其他請求發送。這是一個小示範。

String cookie; 
private static String cookieHeader = "Cookie"; 
private static String Header_SetCookie = "Set-Cookie"; 

public HttpResponse get(String url) { 
    HttpGet getMethod = new HttpGet(url); 
    DefaultHttpClient hc = new DefaultHttpClient(); 
    if(cookie!=null) { 
     getMethod.addHeader(cookieHeader, cookie); 
    } 
    HttpResponse response = hc.execute(getMethod); 
    Header[] headers = response.getAllHeaders(); 
    for (int i = 0; i < headers.length; i++) { 
     Header h = headers[i]; 
     if(h.getName().equals(Header_SetCookie)){ 
      cookie = h.getValue(); 
     } 
    } 
    return response; 
}