[英]Adding a cookie to a HTTP request
我正在嘗試添加帶有GUID的cookie來識別用戶,但是當我在測試腳本上運行時,我的代碼似乎沒有發送任何cookie。
我的Java代碼:
public void search(String q, int o) {
final String query = q;
final int offset = o;
Thread searchThread = new Thread() {
public void run() {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
BasicHttpContext httpContext = new BasicHttpContext();
CookieStore cookieStore = new BasicCookieStore();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
cookieStore.addCookie(new BasicClientCookie("remixsid", guid));
httpClient.setCookieStore(cookieStore);
HttpPost httpPost = new HttpPost("http://192.168.1.43/test/test.php?section=audio&q=" + URLEncoder.encode(query, HTTP.UTF_8) + "&offset=" + offset);
//HttpPost httpPost = new HttpPost("http://vkontakte.ru/gsearch.php?section=audio&q=" + URLEncoder.encode(query, HTTP.UTF_8) + "&offset=" + offset);
HttpResponse response = httpClient.execute(httpPost, httpContext);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.print(responseBody);
} catch(Exception e) {
System.out.println(e.toString());
}
}
};
searchThread.run();
}
我的PHP代碼測試cookie:
<?php
$cookies = $_COOKIE;
if(!count($cookies) > 0)
echo 'No cookies!';
foreach ($cookies as $key=>$val)
echo "$key--> $val";
?>
使用以下代碼檢索GUID:
public void login(String usr, String pass) {
logout();
final String username = usr;
final String password = pass;
Thread loginThread = new Thread() {
public void run() {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://login.vk.com/?act=login&email=" + URLEncoder.encode(username, HTTP.UTF_8) + "&pass=" + URLEncoder.encode(password, HTTP.UTF_8));
httpClient.execute(httpPost);
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for(Cookie cookie : cookies) {
System.out.println("Cookie: " + cookie.toString());
if(cookie.getName().contains("remixsid"))
guid = cookie.getValue();
}
} catch(Exception e) {
System.out.println(e.toString());
System.out.println("An error occured");
if(loginHandler != null)
loginHandler.onLoginError(e);
return;
}
if(!isLoggedIn()) {
System.out.println("User credentials invalid");
if(loginHandler != null)
loginHandler.onLoginInvalidCredentials();
return;
}
if(loginHandler != null)
loginHandler.onLoginSuccess();
System.out.println("Login successfull GUID is: " + guid);
}
};
loginThread.start();
}
手動設置cookie標頭似乎工作...
public void search(String q, int o) {
final String query = q;
final int offset = o;
Thread searchThread = new Thread() {
public void run() {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://192.168.1.43/test/test.php");
//HttpPost httpPost = new HttpPost("http://vkontakte.ru/gsearch.php?section=audio&q=" + URLEncoder.encode(query, HTTP.UTF_8) + "&offset=" + offset);
httpPost.setHeader("Cookie", "remixsid=" + guid);
HttpResponse response = httpClient.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
} catch(Exception e) {
System.out.println(e.toString());
}
}
};
searchThread.start();
}
幾點意見和建議:
BasicClientCookie.setPath()
方法。 /
)時,只有它可用於所有其他服務器端程序。 編輯:
我錯過了域名部分。 您還需要設置cookie域。 假設您的PHP腳本運行在http://mysite/myscript.php
這里mysite
是域(如果您在本地框中運行腳本,則可能是localhost
)。 使用BasicClientCookie.setDomain()
設置cookie域。
要理解這一點,假設您的http-client lib作為瀏覽器與網站交互。 瀏覽器存儲來自許多站點的cookie,但在向特定站點發出請求時,它僅發送從該特定站點收到的cookie。
如果未設置域,則lib不會將cookie發送到該主機。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.