开发者

how to use cookies in HttpsURLConnection in android

actually i am new in Android and now i have to add the cookies in my project. i am using HttpsUrlConnection. here is how i am making request and getting response from a webserver and now i have to add cookies aswell.

   URL url = new URL(strUrl);

 HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

 connection.setRequestMethod("POST");    

 connection.setRequestProperty("Content-Type", 
    "application/soap+xml; charset=utf-8");

    connection.setRequestProperty("Content-Length", ""+ 
 开发者_StackOverflow中文版            Integer.toString(request.getBytes().length));

    connection.setUseCaches (false);
    connection.setDoInput(true);
    connection.setDoOutput(true);


    // send Request...
    DataOutputStream wr = new DataOutputStream (connection.getOutputStream());
 wr.writeBytes (request);
 wr.flush ();
 wr.close ();

 //Get response...
 DataInputStream is = new DataInputStream(connection.getInputStream());    
 String line;
 StringBuffer response = new StringBuffer(); 
 while((line = is.readLine()) != null) {
    response.append(line);
  }
 is.close();
 FileLogger.writeFile("Soap.txt", "RESPONSE: " + methodName + "\n" + response);
 HashMap<String, String> parameters = null;
 try {
   parameters = SoapRequest.responseParser(response.toString(), methodName);
  } catch (ParserConfigurationException e) {
  // TODO Auto-generated catch block
 e.printStackTrace();
 } catch (SAXException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 return parameters;

any help will be appreciative, thanks


You have a tutorial here (is for URLConnection, but HttpsURLConnection is a subclass so it should also work).

Basically you have to do:

connection.setRequestProperty("Cookie", myCookie);

where myCookie has the form "userId=igbrown" if only one or "userId=igbrown; sessionId=SID77689211949; isAuthenticated=true" if many (the separator is semicolon AND whitespace)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜