android - 在 HttpRequest 中发送 Cookie 信息

标签 android

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

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

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

最佳答案

今天,我使用 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();

关于android - 在 HttpRequest 中发送 Cookie 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7133993/

相关文章:

c# - Xamarin,Grpc,无法使用 token 0100002b 解析类型

android - 从 android 中通过 sqlite 数据库进行不适当的搜索

android - 在适配器中设置时未选中复选框

java - 使用文件名填充 ListView

java - 安卓输出显示

android - 扩展 RelativeLayout,并覆盖 dispatchDraw() 以创建可缩放的 ViewGroup

Android: list 中的奇怪错误

android - 解析推送 : Which is the preferred way to uniquely identify a device: installationId or deviceToken?

android - Google Maps SDK 错误在最后一小时开始出现

java - 使用 google API key 的 URL 缩短器