0

I need to set the Host header for a very specific request and this is not being taken into account.

I know this is a restricted header, but how is it possible to do it?

Here's what I'm trying.

HttpURLConnection urlConnection = null;

    try {
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setConnectTimeout(10000);
        urlConnection.setRequestProperty("Host", "test.com");
        urlConnection.setRequestMethod("GET");

        int responseCode = urlConnection.getResponseCode();
        Log.d(TAG, String.format("response code: %d", responseCode));
    } catch (IOException e) {

    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
hpinhal
  • 512
  • 6
  • 22
  • 1
    Possible duplicate of [Can I override the Host header where using java's HttpUrlConnection class?](http://stackoverflow.com/questions/7648872/can-i-override-the-host-header-where-using-javas-httpurlconnection-class) – Oleg Bogdanov Jan 12 '17 at 22:02
  • Indeed. I have set `System.setProperty("sun.net.http.allowRestrictedHeaders", "true")` on the `Application.onCreate()` and it's not working. – hpinhal Jan 13 '17 at 00:13

1 Answers1

0

If I'm not misunderstanding your question perhaps you can do something like this instead of trying to set the host header under your try statement:

URL url = new URL("http://www.test.com/");

The host header will take on the information from whichever URL you supply.

Source: https://developer.android.com/reference/java/net/HttpURLConnection.html

terratunaz
  • 614
  • 3
  • 9
  • 19