-1

I am trying to send Cookie with HttpGet Url. which receive from my service(call by HttpGet), for Authentication. I send it with Url but every time get error message.

like:"User must be Authentificated".

I get following error.

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

Screenshot of my demo with error message. enter image description here

Anyone have an Idea to resolve this problem...

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
  • Can you add the code? Sidenote: You know it's *cancel* instead of *cencel* right? – Rob May 23 '13 at 08:29

1 Answers1

0

The server will send you a Set-Cookie header which you will have to use to get the cookie and send it in all other requests. Here is a small demonstration.

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;
}
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106