Search code examples
androidgethttpurlconnectionhttp-token-authentication

send header value using HttpUrlConnection


I have trying to send response to server using authentication value which, it provides me at login time. The server only accepts requests when that value is sent. However it works well with postman (see snapshot) but not with my code.

code

 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);

I also have tried in that way

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

Solution

  • Try this one

    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);