Android的,如何获得通过的HttpClient从URL的cookie()?如何获得、Android、HttpClient、cookie

由网友(小太阳哟)分享简介:我有一个登录活动,我要创造我的网站登录用户到我的手机应用程序的POST请求。在我的网站上创建post请求我需要的CSRF的cookie作为参数,这意味着我必须首先从我的网址获取Cookie,并创建具有CSRF值我的职务请求后。下面是我的code: HttpClient的客户端=新DefaultHttpClient()...

我有一个登录活动,我要创造我的网站登录用户到我的手机应用程序的POST请求。在我的网站上创建post请求我需要的CSRF的cookie作为参数,这意味着我必须首先从我的网址获取Cookie,并创建具有CSRF值我的职务请求后。

下面是我的code:

  HttpClient的客户端=新DefaultHttpClient();        HttpPost后=新HttpPost(http://192.168.178.163:8080/login/);        尝试{            清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(1);            nameValuePairs.add(新BasicNameValuePair(用户名,XXX));            nameValuePairs.add(新BasicNameValuePair(密码,YYY));            // csrfmiddlewaretoken            字符串资源= NULL;            post.setEntity(新UrlEn codedFormEntity(namevaluepairs中));            HTT presponse响应= client.execute(岗位);            RES = response.toString();            RES = res.replaceAll(\ S +,);            RD的BufferedReader =新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent()));            串线=;            而((行= rd.readLine())!= NULL){                Log.i(线,线);                //System.out.println(line);                如果(line.startsWith(csrftoken =)){                    字符串键= line.substring(5);                    Log.i(密钥,密钥);                }            }        }        赶上(IOException异常五){            txt_Error.setText(e.toString());        } 

任何想法,该怎么办呢?我已经读到CookieSyncManager但我不明白在所有...任何想法或code样品将aprreciate

解决方案

  HttpClient的客户端=新DefaultHttpClient();HttpPost后=新HttpPost(http://192.168.178.163:8080/login/);的CookieStore的CookieStore =新BasicCookieStore();HttpContext的背景下=新BasicHttpContext();context.setAttribute(ClientContext.COOKIE_STORE,的CookieStore);...HTT presponse响应= client.execute(邮局,背景);清单< Cookie和GT;饼干= cookieStore.getCookies();CookieMonster.eat(饼干); // :) 
android studio怎么httpclient

I have a login activity and I have to create a post request for my website to login the user into my mobile app. To create post requests on my website I need the csrf cookie as parameter, it means I have first to get the cookie from my URL and after create my post request with the csrf value.

Here is my code:

        HttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost("http://192.168.178.163:8080/login/");

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("username", "xxx"));
            nameValuePairs.add(new BasicNameValuePair("password", "yyy"));
            //csrfmiddlewaretoken
            String res = null;

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = client.execute(post);
            res = response.toString();
            res = res.replaceAll("s+","");
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            String line = "";
            while ((line = rd.readLine()) != null) {
                Log.i("line", line);
                //System.out.println(line);
                if (line.startsWith("csrftoken=")) {
                    String key = line.substring(5);
                    Log.i("key", key);
                }

            }
        }
        catch (IOException e) {
            txt_Error.setText(e.toString());
        }

Any idea how to do it? I already read about CookieSyncManager but I didnt understand at all... Any idea or code sample will be aprreciate

解决方案

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.178.163:8080/login/");
CookieStore cookieStore = new BasicCookieStore();
HttpContext context = new BasicHttpContext();
context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
...
HttpResponse response = client.execute(post, context);
List<Cookie> cookies = cookieStore.getCookies();
CookieMonster.eat(cookies); // :)

阅读全文

相关推荐

最新文章