Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpURLConnection GET request with http-header "Accept"

I have read some relative questions, but unfortunately they do not answer my one, since I have specific requirements.

Maybe it's a dumb question, but how can I request (GET) a JSON-response using httpURLConnection and the http-header "Accept"?

I found a snippet in the documentation, but I'm not sure how to do it though.

Accept = "Accept" ":" #( media-range [ accept-params ] )
like image 966
0x44656E6E79 Avatar asked May 16 '15 11:05

0x44656E6E79


1 Answers

I can't see what programming language you're talking about, so I assume it's Java since this is the first thing that pops up when searching for httpURLConnection.

If that's the case, then you can write

URL url = new URL("https://stackoverflow.com");
HttpURLConnection urlConnection = (HttpURLConnection)  url.openConnection();
urlConnection.setRequestProperty("Accept", "application/json");
try {
    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    ...
} finally {
    urlConnection.disconnect();
}

Source

like image 194
Jan Sommer Avatar answered Oct 22 '22 13:10

Jan Sommer