从HTTP请求设置cookie(Set cookie from HTTP Request)

编程入门 行业动态 更新时间:2024-10-25 08:28:28
从HTTP请求设置cookie(Set cookie from HTTP Request)

我使用HttpRequest获得正确的登录工作。 它在我的吐司中打印登录页面的正确html格式(仅用于测试)。 现在我想根据该请求设置一个cookie。 这怎么可能? 如果有必要,我可以提供一些代码。

我已经了解了CookieManager类,但我怎样才能成功完成它?

提前致谢!

我的代码:

public String getPostRequest(String url, String user, String pass) { HttpClient postClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse response; try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("login", user)); nameValuePairs.add(new BasicNameValuePair("pass", pass)); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); response = postClient.execute(httpPost); if(response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String result = convertStreamToString(instream); instream.close(); return result; } } } catch (Exception e) {} Toast.makeText(getApplicationContext(), "Connection failed", Toast.LENGTH_SHORT).show(); return null; } private String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }

嗯,这就是它。 convertStreamToString()函数将InputStream转换为一个String(纯HTML代码),我“吐司”只是为了测试它(所以它工作),所以代码工作正常。 现在设置cookie。 :-)

这就是我现在所达到的目标:

// inside my if (entity != null) statement List<Cookie> cookies = postClient.getCookieStore().getCookies(); String result = cookies.get(1).toString(); return result;

当我登录时,CookieList id 1包含一个值,否则该值是标准值。 所以现在我知道价值的差异,但我怎么能继续?

I have get a correct login using HttpRequest to work. It prints the correct html form of the logn page in my toast (just for testing). Now I want to set a cookie from that request. How is this possible? If it necessary I can provide some code.

I already know about the CookieManager class, but how can I successfully do it?

Thanks in advance!

My code:

public String getPostRequest(String url, String user, String pass) { HttpClient postClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse response; try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("login", user)); nameValuePairs.add(new BasicNameValuePair("pass", pass)); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); response = postClient.execute(httpPost); if(response.getStatusLine().getStatusCode() == 200) { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String result = convertStreamToString(instream); instream.close(); return result; } } } catch (Exception e) {} Toast.makeText(getApplicationContext(), "Connection failed", Toast.LENGTH_SHORT).show(); return null; } private String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }

Well, this is pretty much it. convertStreamToString() function converts the InputStream into a String (plain HTML code), which I "toast" out to just test it (so it work), so the code is working though. Now to set the cookie. :-)

This is what I've reached for now:

// inside my if (entity != null) statement List<Cookie> cookies = postClient.getCookieStore().getCookies(); String result = cookies.get(1).toString(); return result;

When I have logged in, the CookieList id 1 contains a value, otherwise the value is standard. So for now I know the difference in value, but how can I continue?

最满意答案

我认为Android附带了Apache HttpClient 4.0。

您可以从HttpClient教程中查看第3章HTTP状态管理主题。

你也可以在SO上提到类似的问题:

Android项目使用httpclient - > http.client(apache),post / get方法

如何使用Android和/或Java中的HttpClient管理cookie?

另请查看此示例以了解用法: http : //svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java

I think Android ships with Apache HttpClient 4.0.

You can check Chapter 3. HTTP state management topic from HttpClient Tutorial.

You can also refer similar questions on SO:

Android project using httpclient --> http.client (apache), post/get method

How do I manage cookies with HttpClient in Android and/or Java?

Also Check this example for usage: http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientFormLogin.java

更多推荐

本文发布于:2023-08-01 04:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353815.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:cookie   HTTP   Request   Set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!