android - 使用 HttpUrlConnection 发送 header 值

标签 android get httpurlconnection http-token-authentication

我尝试使用身份验证值向服务器发送响应,它在登录时提供给我。服务器仅在发送该值时才接受请求。然而,它适用于 postman (see snapshot)但不是我的代码。

代码

 URL url = new URL(strings[0]);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            String token = "" + new String(accessToken);
            con.setRequestMethod("GET");
            con.setRequestProperty("AuthToken", token);
            con.setReadTimeout(15000);
            con.setConnectTimeout(15000);
            con.setDoOutput(false);

我也试过这种方法

String token = "AuthToken :" + new String(accessToken);
con.setRequestProperty("Authorization", token);

最佳答案

试试这个

URL url = new URL(strings[0]);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        String token = "" + new String(accessToken);
        con.setRequestMethod("GET");
        con.setRequestProperty("AuthToken", token);
        con.setReadTimeout(15000);
        con.setConnectTimeout(15000);
        con.setDoOutput(false);

关于android - 使用 HttpUrlConnection 发送 header 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320017/

相关文章:

Java POST HTTPsUrlConnection 返回 200 响应

java - 上传大文件时暂停/恢复 Httpurl 连接 - Android

java - Android TTS 声音泄露服务连接和语音已弃用

java - 在第二个 Activity 中分别收集和显示动态 editTexts 的值

c# - Http Get 请求 GetResponse 错误

PHP 提交变量 'passkey' 时会停止更新数据库

android - 使用 HttpURLConnection 向服务器发送请求时缺少参数

android - Android 中的 System.exit()

android - google-play-service-lib 中缺少 BaseGameUtils

php - 如何在 get 语句上添加 order by 选项