Java – HTTP header is used to “accept” httpurlconnection get request

I have read some related questions, but unfortunately they didn't answer my questions because I had specific requirements

Maybe this is a stupid question, but how do I use httpurlconnection and HTTP header "accept" request (get) JSON response?

I found a fragment in the document, but I don't know how to do it

Accept = "Accept" ":" #( media-range [ accept-params ] )

Solution

I can't see what programming language you're talking about, so I think it's Java because it's the first thing that pops up when searching httpurlconnection

If so, 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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>