简体   繁体   English

HttpClient不发送cookie

[英]HttpClient not sending cookies

I'm trying to use the Reddit API ( https://github.com/reddit/reddit - www.reddit.com/dev). 我正在尝试使用Reddit API( https://github.com/reddit/reddit-www.reddit.com/dev )。

Here is my HttpClientHelper : 这是我的HttpClientHelper

public HttpClientHelper(String path)
{
    this.url = "www.reddit.com";
    this.path = path;

    if(httpClient == null)
    {
        httpClient = new DefaultHttpClient();
    }
}

public void addParamForGet(String key, String value)
{
    dataGet.add(new BasicNameValuePair(key, value));
}

public void addParamForPost(String key, String value)
{
    dataPost.add(new BasicNameValuePair(key, value));
}

public HttpResponse executePost()
{
    HttpResponse response = null;

    try {
        uri = URIUtils.createURI(METHOD, url, PORT, path,
                dataGet == null ? null : URLEncodedUtils.format(dataGet, "UTF-8"), null);

        HttpPost httpPost = new HttpPost(uri);
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

        httpPost.setEntity(new UrlEncodedFormEntity(dataPost, HTTP.UTF_8));

        response = httpClient.execute(httpPost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (URISyntaxException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    return response;
}

public HttpResponse executeGet()
{
    try {
        uri = URIUtils.createURI(METHOD, url, PORT, path,
                dataGet == null ? null : URLEncodedUtils.format(dataGet, "UTF-8"), null);
    } catch (URISyntaxException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    HttpGet httpget = new HttpGet(uri);
    HttpResponse response = null;

    try {
        response = httpClient.execute(httpget);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return response;
}

When I try to login on Reddit, it works: 当我尝试登录Reddit时,它可以工作:

    HttpClientHelper client = new HttpClientHelper(Endpoints.User.login);

    client.addParamForPost("api_type", "json");
    client.addParamForPost("user", username);
    client.addParamForPost("passwd", password);
    client.addParamForPost("rem", String.valueOf(true));

    client.executePost();

Result: {"json":{"data":{"cookie":"30310021,2014-10-27T16:02:27,300937022ed465f695747a2aa7fd**********","need_https":false,"modhash":"9ch7btilr85ce7c2a427ef87bdb422132f738c3a1**********"},"errors":[]}} 结果: {"json":{"data":{"cookie":"30310021,2014-10-27T16:02:27,300937022ed465f695747a2aa7fd**********","need_https":false,"modhash":"9ch7btilr85ce7c2a427ef87bdb422132f738c3a1**********"},"errors":[]}}

Here are the cookies returned (overshadowed): 以下是返回的Cookie(已被遮盖):

    List<Cookie> cookies = client.getHttpClient().getCookieStore().getCookies();
    for (int i = 0; i < cookies.size(); i++) {
        Log.d("", cookies.get(i).toString());
    }

    [version: 0][name: __cfduid][value: d****7d2bfa753ef32b2f2861c117b8c141444******][domain: .reddit.com][path: /][expiry: Mon Dec 23 23:50:00 GMT 2019]
    [version: 0][name: reddit_session][value: 30310021%2C2014-10-27T15%3A18%3A39%2C5**08054f8fa07d82aa0ac027a2f**********][domain: reddit.com][path: /][expiry: Thu Dec 31 23:59:57 GMT 2037]

On the following request to retrieve the user data /api/me.json , it fails: 在以下请求中检索用户数据/api/me.json ,它失败:

    HttpClientHelper client = new HttpClientHelper(Endpoints.User.me);
    client.executeGet();

Result: {} 结果: {}

But, when I print the cookies, the result are the same (this is expected, as I use the same instance of DefaultHttpClient): 但是,当我打印cookie时,结果是相同的(这是预期的,因为我使用DefaultHttpClient的相同实例):

    List<Cookie> cookies = client.getHttpClient().getCookieStore().getCookies();
    for (int i = 0; i < cookies.size(); i++) {
        Log.d("", cookies.get(i).toString());
    }

    [version: 0][name: __cfduid][value: d****7d2bfa753ef32b2f2861c117b8c141444******][domain: .reddit.com][path: /][expiry: Mon Dec 23 23:50:00 GMT 2019]
    [version: 0][name: reddit_session][value: 30310021%2C2014-10-27T15%3A18%3A39%2C5**08054f8fa07d82aa0ac027a2f**********][domain: reddit.com][path: /][expiry: Thu Dec 31 23:59:57 GMT 2037]

What am I doing wrong? 我究竟做错了什么?

Thanks. 谢谢。

Old post but... 旧帖子但是...

You didn't indicate the nature of the failure on retrieving user data. 您没有指出检索用户数据失败的性质。

One thing I observe is that the reddit_session cookie has a domain of 'reddit.com', whereas the other has domain '.reddit.com'. 我观察到的一件事是reddit_session cookie的域为“ reddit.com”,而另一域的域为“ .reddit.com”。 The former would only be sent back to the server when you use 'reddit.com as opposed to 'www.reddit.com', whereas the latter would be sent for all sub-domains. 仅当您使用“ reddit.com”而不是“ www.reddit.com”时,前者才会发送回服务器,而后者将针对所有子域发送。 Perhaps this was the cause of the failure. 也许这是失败的原因。

I am puzzled that you got those cookies back though if you were in fact using a domain of 'www.reddit.com' for the original request. 我感到困惑的是,即使实际上您使用的域名是“ www.reddit.com”作为原始请求,您也得到了这些Cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM