如何在android中的HttpsURLConnection中使用cookie

发布于 2024-10-11 12:06:16 字数 1447 浏览 3 评论 0原文

实际上我是 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;

任何帮助将不胜感激,谢谢

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", ""+ 
             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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2024-10-18 12:06:16

您在此处有一个教程(适用于URLConnection,但是 HttpsURLConnection 是一个子类,因此它也应该可以工作)。

基本上你必须这样做:

connection.setRequestProperty("Cookie", myCookie);

其中 myCookie 的形式为 "userId=igbrown" 如果只有一个或 "userId=igbrown; sessionId=SID77689211949; isAuthenticated=true" 如果很多(分隔符是分号和空格)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文