【问题标题】:Sending Cookie info in HttpRequest在 HttpRequest 中发送 Cookie 信息
【发布时间】:2018-06-05 23:52:53
【问题描述】:

我想调用一个需要身份验证 cookie 的 Web 服务。

我有 cookie 的名称和值。但我不知道如何在请求中注入cookie。

能否请您提供有关如何执行此操作的代码示例。

【问题讨论】:

    标签: android


    【解决方案1】:

    今天,我使用 HttpUrlConnection 解决了同样的问题:

            CookieManager cookieManager = CookieManager.getInstance();
            String cookieString = cookieManager.getCookie(SystemConstants.URL_COOKIE); 
            URL url = new URL(urlToServer);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Cookie", cookieString);
            connection.connect();
            OutputStream out = connection.getOutputStream();
            out.write(data.getBytes());
            out.flush();
            out.close();
    

    【讨论】:

      【解决方案2】:

      如果您使用 (Http)UrlConnection 进行请求,那么您可以使用 CookieManager 来处理 cookie。 Here 是一篇关于如何使用它的文章。

      【讨论】:

        【解决方案3】:

        没有向HttpRequest 添加 cookie 的方法,但您可以设置标头或参数。

        Cookie 像这样添加到 HttpServletResponse:

        HttpServletResponse response; //initialized or passed in
        Cookie cookie = new Cookie("myname", "myvalue");
        response.addCookie(cookie);
        

        【讨论】:

          【解决方案4】:
          HttpClient httpClient = new DefaultHttpClient();
          
          CookieStore cookieStore = new BasicCookieStore();
          Cookie cookie = new BasicClientCookie("name", "value");
          cookieStore.addCookie(cookie);
          
          HttpContext localContext = new BasicHttpContext();
          localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
          
          HttpGet httpGet = new HttpGet("http://www.domain.com/"); 
          
          HttpResponse response = httpClient.execute(httpGet, localContext);
          

          【讨论】:

            【解决方案5】:

            您可以使用droidQuery来处理请求:

            $.ajax(new AjaxOptions().url("http://www.example.com")
                                    .type("POST")
                                    .dataType("json")
                                    .data("data to post")
                                    .cookies($.map($.entry("key", "value"))));
            

            droidQuery 还内置了使用标准 HTTP 身份验证方法的身份验证:

            $.ajax(new AjaxOptions().url("http://www.example.com")
                                    .type("POST")
                                    .dataType("json")
                                    .data("data to post")
                                    .username("myusername")
                                    .password("myPassword"));
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-05-03
              • 2019-08-10
              相关资源
              最近更新 更多