HttpPost与cookies请求 [英] HttpPost request with cookies

查看:216
本文介绍了HttpPost与cookies请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要提出两个请求到服务器。在第一个请求我送参数用户ID = 1,服务器返回一个cookie _session_ID。现在我不带参数发送一个第二请求,结果应该是相同的第一次。但是,cookie不会发送。为什么呢?

这是我的code:

 公共类服务器
{
    静态的HttpClient HttpClient的;
    静态HttpPost httppost;
    静态字符串JsonString;
    静态的Htt presponse响应;
    静态列表<&的NameValuePair GT; namevaluepairs中;
    静态的CookieStore的CookieStore;
    静态的HttpContext localContext;
    静态列表< Cookie和GT;曲奇饼;
    静态INT cookieSize;    公共静态无效清除()
    {
        尝试
        {
            的CookieStore =新BasicCookieStore();
            localContext =新BasicHttpContext();
            localContext.setAttribute(ClientContext.COOKIE_STORE,的CookieStore);            HttpClient的=新DefaultHttpClient();
            httppost =新HttpPost(http://mobile-app-storage.herokuapp.com/test);
            namevaluepairs中=新的ArrayList<&的NameValuePair GT; (2);
            nameValuePairs.add(新BasicNameValuePair(用户ID,1));
            httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
            响应= httpclient.execute(httppost,localContext);
            JsonString = ReadFromServer(response.getEntity()的getContent());            Log.e(请求,1);
            饼干= cookieStore.getCookies();
            cookieSize = cookies.size();
            的for(int i = 0; I< cookieSize;我++)
            {
                Log.v(曲奇+ I,名称:+ cookies.get(ⅰ)的ToString());
            }            //没有用户ID第二个请求。必须是相同的结果第一            HttpClient的=新DefaultHttpClient();
            httppost =新HttpPost(http://mobile-app-storage.herokuapp.com/test);
            nameValuePairs.clear();
            httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
            响应= httpclient.execute(httppost);
            JsonString = ReadFromServer(response.getEntity()的getContent());            Log.e(请求,2);
            饼干= cookieStore.getCookies();
            cookieSize = cookies.size();
            的for(int i = 0; I< cookieSize;我++)
            {
                Log.v(曲奇+ I,名称:+ cookies.get(ⅰ)的ToString());
            }
        }
        赶上(Throwable的五){Log_.Error(E);}
    }
}


解决方案

在第二个电话,你不这样做。

  httpclient.execute(httppost,localContext);

I need to make two requests to the server. In the first request I send parameter userID = 1 and the server returns a cookie _session_ID. Now I send a second request with no parameters and the result should be the same as the first time. But the cookie isn't sent. Why?

This is my code:

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

解决方案

In the second call you don't do

httpclient.execute(httppost, localContext);

这篇关于HttpPost与cookies请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆