gpt4 book ai didi

android - 带有 cookie 的 HttpPost 请求

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:10:31 24 4
gpt4 key购买 nike

我需要向服务器发出两个请求。在第一个请求中,我发送参数 userID = 1,服务器返回一个 cookie _session_ID。现在我发送第二个没有参数的请求,结果应该和第一次一样。但是没有发送 cookie。为什么?

这是我的代码:

public class Server
{
static HttpClient httpclient;
static HttpPost httppost;
static String JsonString;
static HttpResponse response;
static List <NameValuePair> nameValuePairs;
static CookieStore cookieStore;
static HttpContext localContext;
static List<Cookie> cookies;
static int cookieSize;

public static void Clear()
{
try
{
cookieStore = new BasicCookieStore();
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://mobile-app-storage.herokuapp.com/test");
nameValuePairs = new ArrayList <NameValuePair> (2);
nameValuePairs.add(new BasicNameValuePair("userID", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost, localContext);
JsonString = ReadFromServer(response.getEntity().getContent());

Log.e("Request", "1");
cookies = cookieStore.getCookies();
cookieSize = cookies.size();
for (int i = 0; i < cookieSize; i++)
{
Log.v("Cookie "+ i, "name: "+cookies.get(i).toString());
}

// Second request without userID. Must be same result as first

httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://mobile-app-storage.herokuapp.com/test");
nameValuePairs.clear();
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
JsonString = ReadFromServer(response.getEntity().getContent());

Log.e("Request", "2");
cookies = cookieStore.getCookies();
cookieSize = cookies.size();
for (int i = 0; i < cookieSize; i++)
{
Log.v("Cookie "+ i, "name: "+cookies.get(i).toString());
}
}
catch (Throwable e) {Log_.Error(e);}
}
}

最佳答案

在第二次通话中你不这样做

httpclient.execute(httppost, localContext);

关于android - 带有 cookie 的 HttpPost 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16608665/

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