2010-01-13 60 views
0

我正在構建一個Android應用程序,它應該在我的網站上執行GET以獲取兩個Cookie,然後使用這些Cookie對同一站點執行發佈。爲什麼HttpClient不工作,但HttpUrlConnenction發佈數據時形式

正如我剛纔提到的那樣,我使用org.apache.http.client.HttpClient來執行這個操作。

String requiredCookies = ""; 
HttpContext localContext = null; 

System.out.println("------------------GET----------------------"); 
HttpClient httpClient = new DefaultHttpClient(); 
HttpGet get = new HttpGet("www.mysitegeturl.com"); 

//Creating a local instance of cookie store. 
CookieStore cookieJar = new BasicCookieStore(); 

// Creating a local HTTP context 
localContext = new BasicHttpContext(); 

// Bind custom cookie store to the local context 
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar); 

HttpResponse response; 
try { 
    response = httpClient.execute(get, localContext); 
HttpEntity entity = response.getEntity(); 
System.out.println(response.getStatusLine()); 
if (entity != null) { 
    System.out.println("Response content length: " + entity.getContentLength()); 
} 

//Do this so that Java.net impl should work 
List<Cookie> cookies = cookieJar.getCookies(); 
for (int i = 0; i < cookies.size(); i++) { 
    requiredCookies += cookies.get(i).getName()+"="+cookies.get(i).getValue()+";"; 
} 

if (entity != null) { 
    entity.consumeContent(); 
} 

} catch (ClientProtocolException e1) { 
     // TODO Auto-generated catch block 
e1.printStackTrace(); 
} catch (IOException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 
System.out.println("------------------GET-END---------------------"); 

到目前爲止好。不介意requiredCookies行了,它會在Java.net IMPL使用,因爲我不能讓HttpClient的一個工作=( 讓我們來看看在非工作HttpClient的後一部分。

System.out.println("------------------HttpClient - POST----------------------"); 
HttpPost post = new HttpPost("www.mysiteposturl.com"); 

//Params  
HttpParams params = new BasicHttpParams(); 

params.setParameter("foo", "post"); 
params.setParameter("bar", "90"); 
params.setParameter("action", "search"); 

post.setParams(params); 
post.setHeader("Content-Type", "application/x-www-form-urlencoded"); 

try { 
    HttpResponse response2 = httpClient.execute(post, localContext); 
    System.out.println(response2.getStatusLine()); 
} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
System.out.println("------------------POST END---------------------"); 

現在發生的事情是,我用localContext執行一個POST存儲cookie的地方,這是行不通的,我得到了一個HTTP/1.1 401 No session,因爲我沒有運氣,所以我嘗試了另一種方法(java .net.HttpURLConnection)。記住我仍然使用相同的GET部分

URL url = new URL("www.mysiteposturl"); 
HttpURLConnection connection = null; 

String dataString = "bar=90&foo=post&action=search"; 

try { 
connection = (HttpURLConnection)url.openConnection(); 
connection.setRequestProperty("Cookie", requiredCookies); 
//Set to POST 
connection.setDoOutput(true); 
Writer writer = new OutputStreamWriter(connection.getOutputStream()); 
writer.write(dataString); 
writer.flush(); 
writer.close(); 
connection.connect(); 

if (connection.getResponseCode() == 200 || connection.getResponseCode() == 201) { 
    System.out.println(connection.getContent().toString()); 
} else { 
    System.out.println("Error"); 
} 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (MalformedURLException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

System.out.println("------------------POST END---------------------"); 

和中提琴200顯示,一切工作就像一個魅力。你什麼區你覺得呢?有人可以給我一個答案,因爲我無法弄清楚。

+0

Cookie如何在不同的域中工作? – 2010-01-13 20:22:54

回答

2

問題似乎是在設置中有兩個不同的主機名。這將導致HTTP客戶端無法爲其他主機發送Cookie。您可以嘗試更改Cookie存儲區中Cookie的域,或使用相同的主機進行GET和POST。另外,您可以像在HttpURLConnection示例中那樣手動將Cookie添加到HTTP客戶端的頭文件中。

+0

是的,這是問題。爲了解決它,我更改了Cookie的域名。謝謝。 – jakob 2010-01-16 13:48:24

0

我想這是一個錯誤,你使用兩個完全不同的域名爲你的兩個請求—即你試圖掩蓋你的真實網址?如果沒有,那麼這就是爲什麼你沒有得到任何cookie。如果只是試圖掩蓋您的網址,那麼這就是爲什麼example.com存在。 :)

或者,這完全是把我的頭從代碼我上週寫—的頂部它好工作在多個入眼,職位和子域:

DefaultHttpClient httpClient = new DefaultHttpClient(); 
CookieStore cookieJar = new BasicCookieStore(); 
httpClient.setCookieStore(cookieJar); 

即我明確地使用DefaultHttpClient,我相信這些額外的get/setter的cookie存儲。我不認爲我也使用任何上下文對象。

相關問題