实际上我是Android的新手,现在我必须在我的项目中添加cookie。我正在使用HttpsUrlConnection。这是我如何提出请求并从网络服务器获得回复,现在我还要添加cookie。
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;
感谢任何帮助,谢谢
答案 0 :(得分:14)
您有一个教程here(适用于URLConnection,但HttpsURLConnection是一个子类,所以它也应该有效。)
基本上你必须这样做:
connection.setRequestProperty("Cookie", myCookie);
其中myCookie
的格式为"userId=igbrown"
,如果只有一个"userId=igbrown; sessionId=SID77689211949; isAuthenticated=true"
,如果很多(分隔符是分号和空格)