gpt4 book ai didi

java - 向 HTTP 请求添加 cookie

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:00:01 26 4
gpt4 key购买 nike

我正在尝试添加一个带有 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();
}

我用于测试 cookie 的 PHP 代码:

<?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 header 似乎可行...

    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();
}

关于java - 向 HTTP 请求添加 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10859766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com