【问题标题】:How to send Cookie with HttpGet Url?如何使用 HttpGet Url 发送 Cookie?
【发布时间】:2013-05-16 05:33:38
【问题描述】:

我正在尝试使用 HttpGet Url 发送 Cookie。从我的服务(由 HttpGet 调用) 接收,用于身份验证。我用 Url 发送它,但每次都会收到错误消息。

like:"用户必须经过身份验证"。

我收到以下错误

05-15 10:28:17.623: I/System.out(18393): <root>
05-15 10:28:17.623: I/System.out(18393): <status>0</status>
05-15 10:28:17.623: I/System.out(18393): <error>User must be Authentificated</error>
05-15 10:28:17.623: I/System.out(18393): </root>
05-15 10:28:17.623: I/System.out(18393): response =: org.apache.harmony.xml.dom.DocumentImpl@41bff838
05-15 10:28:17.623: I/System.out(18393): sts value =: 0
05-15 10:28:17.623: I/System.out(18393): print customer actived bid api sts values 1 che...

我的演示屏幕截图,带有错误消息。

任何人都有解决此问题的想法...

【问题讨论】:

  • 可以添加代码吗?旁注:你知道它是 cancel 而不是 cencel 对吗?

标签: android cookies xml-parsing apache-commons-httpclient


【解决方案1】:

服务器将向您发送一个Set-Cookie 标头,您必须使用该标头来获取 cookie 并在所有其他请求中发送它。这是一个小演示。

String cookie;
private static String cookieHeader = "Cookie";
private static String Header_SetCookie = "Set-Cookie";

public HttpResponse get(String url) {
    HttpGet getMethod = new HttpGet(url);
    DefaultHttpClient hc = new DefaultHttpClient();
    if(cookie!=null) {
        getMethod.addHeader(cookieHeader, cookie);
    }
    HttpResponse response = hc.execute(getMethod);
    Header[] headers = response.getAllHeaders();
    for (int i = 0; i < headers.length; i++) {
        Header h = headers[i];
        if(h.getName().equals(Header_SetCookie)){
            cookie = h.getValue();
        }
    }
    return response;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 2015-03-14
    • 2010-09-23
    • 2013-04-06
    相关资源
    最近更新 更多