【问题标题】:Android set a cookie using org.apache.http.legacyAndroid 使用 org.apache.http.legacy 设置 cookie
【发布时间】:2017-07-11 03:28:47
【问题描述】:

我正在尝试在 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'
}

在提出 get 或 post 请求时,为了得到响应,对我来说没问题,但发送一个简单的 cookie 似乎是一件非常奇怪的事情(考虑到 org.apache.http.legacy 在互联网上关于 cookie)。谢谢!

【问题讨论】:

    标签: android apache cookies


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 2015-12-03
      相关资源
      最近更新 更多