Setting custom header using HttpURLConnection
<p>I am simply making a <code>GET</code> request to a Rest API using <code>HttpURLConnection</code>.</p>

<p>I need to add some custom headers but I am getting <code>null</code> while trying to retrieve their values.</p>

<p><strong>Code:</strong></p>

<pre><code>URL url;
try {
    url = new URL("http://www.example.com/rest/");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    // Set Headers
    conn.setRequestProperty("CustomHeader", "someValue");
    conn.setRequestProperty("accept", "application/json");

    // Output is null here &lt;--------
    System.out.println(conn.getHeaderField("CustomHeader"));

    // Request not successful
    if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new RuntimeException("Request Failed. HTTP Error Code: " + conn.getResponseCode());
    }

    // Read response
    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    StringBuffer jsonString = new StringBuffer();
    String line;
    while ((line = br.readLine()) != null) {
        jsonString.append(line);
    }
    br.close();
    conn.disconnect();
} catch (IOException e) {
    e.printStackTrace();
}
</code></pre>

<p>What am I missing?</p>