我正在尝试在Http请求中设置cookie,但我无法弄清楚如何实现它:
HttpGet getReq = new HttpGet("https://www.myexample.com");
getReq.setHeader("mycookie", "customvalue123");
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(getReq);
result = EntityUtils.toString(response.getEntity());
Log.e("RESPONSE", "GET RESPONSE: " + result);
我的图书馆具体是这样的:
android {
useLibrary 'org.apache.http.legacy'
}
说到获取或发布请求,获取响应,对我来说没问题,但发送一个简单的cookie似乎是一个非常奇怪的事情(考虑到没有关于 org.apache.http。遗产 在互联网上有关cookie的信息)。谢谢!
答案 0 :(得分:1)
好的,我发现了问题。
我在这里发布,因此对于那些遇到相同主题的人来说非常有用。 使用org.apache.http.legacy库设置cookie的正确方法是:
HttpGet getReq = new HttpGet("https://www.myexample.com");
getReq.setHeader("Cookie", "mycookie = customvalue123; mycookie2 = custom2");
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(getReq);
result = EntityUtils.toString(response.getEntity());
Log.e("RESPONSE", "GET RESPONSE: " + result);