send header value using HttpUrlConnection

user6587943 picture user6587943 · Apr 10, 2017 · Viewed 14.7k times · Source

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

Answer

Safa picture Safa · Apr 10, 2017

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