How to use cookies in httpsURLConnection in android

I'm actually new to Android, and now I need to add cookies to my project. I am using HttpsUrlConnection. this is how I make a request and get a response from the web server, and now I also need to add cookies.

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", ""+ 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 would be appreciated thanks

+8
android cookies
source share
1 answer

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

Basically you need:

 connection.setRequestProperty("Cookie", myCookie); 

where myCookie has the form "userId=igbrown" if only one or "userId=igbrown; sessionId=SID77689211949; isAuthenticated=true" if many (the delimiter is a semicolon And spaces)

+14
source

Source: https://habr.com/ru/post/650861/


All Articles