在Htt的prequest发送的Cookie信息 [英] Sending Cookie info in HttpRequest

查看:95
本文介绍了在Htt的prequest发送的Cookie信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要调用一个需要验证cookie的Web服务。

我有cookie的名称和值。但我不知道烫到在请求中的Cookie注入。

您可以请给我提供关于如何做到这一点code样本。

解决方案

  HttpClient的HttpClient的=新DefaultHttpClient();

CookieStore的CookieStore的=新BasicCookieStore();
饼干饼干=新BasicClientCookie(名,价值);
cookieStore.addCookie(饼干);

HttpContext的localContext =新BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE,CookieStore的);

HTTPGET HTTPGET =新HTTPGET(http://www.domain.com/);

HTT presponse响应= httpClient.execute(HTTPGET,localContext);
 

I want to call a web service that requires an authentication cookie.

I have the cookie name and value. but I don't know hot to inject the cookie in the request.

can you please provide me with a code sample on how to do this.

解决方案

HttpClient httpClient = new DefaultHttpClient();

CookieStore cookieStore = new BasicCookieStore();
Cookie cookie = new BasicClientCookie("name", "value");
cookieStore.addCookie(cookie);

HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpGet httpGet = new HttpGet("http://www.domain.com/"); 

HttpResponse response = httpClient.execute(httpGet, localContext);

这篇关于在Htt的prequest发送的Cookie信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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