Server not Recognising Cookie From Android Phone

I am trying to connect to a drupal server. I have been able to do this in the past but the drupal developers now require me to add another cookie. The server does not register the cookie that I am trying to send below. Can anybody see why?

public static void maybeCreateHttpClient() {
    if (mHttpClient == null) {
        mHttpClient = new DefaultHttpClient();
        final HttpParams params = mHttpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params,
            REGISTRATION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, REGISTRATION_TIMEOUT);
        ConnManagerParams.setTimeout(params, REGISTRATION_TIMEOUT);



        BasicCookieStore cookieStore = new BasicCookieStore();
        ClientCookie cookie = new BasicClientCookie("aml", key);

        cookieStore.addCookie(cookie);

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


    }
}

It turns out that, I didn't need to use cookie, cookstore or httpcontent. As far as I can understand these are mainly used to managed cookies coming from the server.

I managed to solve my problem by setting a header like so

    String key = "whatever";

    post.addHeader("Cookie", "aml=" + key);

This can be used on httppost, httpget or httput.

That took me a few days for such a simple solution. I hope it helps somebody else out


Oh, well if you're looking for why the cookies weren't added, you weren't getting or adding the cookie store from the http client. You should get the cookie store from the client, add your cookies, and then execute your request.

链接地址: http://www.djcxy.com/p/56268.html

上一篇: 如何检测连接的USB打印机是打开还是关闭?

下一篇: 服务器无法识别来自Android手机的Cookie