2011-01-10 10 views
8

in realtà sono nuovo in Android e ora devo aggiungere i cookie nel mio progetto. sto usando HttpsUrlConnection. ecco come sto facendo richiesta e ricevendo risposta da un server web e ora devo aggiungere anche i cookie.come utilizzare i cookie in HttpsURLConnection in Android

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; 

qualsiasi aiuto sarà riconoscente, grazie

risposta

14

Hai un tutorial here (è per URLConnection, ma HttpsURLConnection è una sottoclasse quindi dovrebbe funzionare anche).

Fondamentalmente si hanno a che fare:

connection.setRequestProperty("Cookie", myCookie); 

dove myCookie ha la forma "userId=igbrown" se solo uno o "userId=igbrown; sessionId=SID77689211949; isAuthenticated=true" se molti (il separatore è punto e virgola e gli spazi bianchi)

Problemi correlati