-1

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

比如:“用户必须经过身份验证”。

我收到以下错误

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...

带有错误消息 的演示的屏幕截图。在此处输入图像描述

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

4

1 回答 1

0

服务器将向您发送一个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;
}
于 2013-05-23T09:05:31.750 回答