Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLConnection setRequestProperty vs addRequestProperty

Lets say I'm talking HTTP to a web server, and I will Accept html or text, but prefer html. In other words, the header should say (I think!)

Accept: text/html, text/*

I'm using Java, so I have a URLConnection. Should I use:

myUrlConnction.setRequestProperty("Accept", "text/html");
myUrlConnction.addRequestProperty("Accept", "text/*");

or

myUrlConnction.setRequestProperty("Accept", "text/html, text/*");

or are they equivalent???

In general, most of the third party code I see doesn't seem to worry much about ordering or multiple values of these headers, so I'm wondering how it ends up working.

like image 946
user949300 Avatar asked Jul 12 '12 19:07

user949300


People also ask

What is the difference between URLConnection and HttpURLConnection?

URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.

What is addRequestProperty?

addRequestProperty(String key, String value) Adds a general request property specified by a key-value pair. abstract void. connect() Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

What is meant by URL connection?

URLConnection is an abstract class whose subclasses form the link between the user application and any resource on the web. We can use it to read/write from/to any resource referenced by a URL object. There are mainly two subclasses that extend the URLConnection class.

Which of the following methods is used to create a connection using URLConnection classes to a particular URL?

The connection object is created by invoking the openConnection method on a URL. The setup parameters and general request properties are manipulated. The actual connection to the remote object is made, using the connect method.


2 Answers

The basic difference between setRequestProperty and addRequestProperty is:-

  1. setRequestProperty>> Sets the general request property. If a property with the key already exists, overwrite its value with the new value.

  2. addRequestProperty >> Adds a general request property specified by a key-value pair. This method will not overwrite existing values associated with the same key.

For more information browse the api doc

like image 75
lambodar Avatar answered Sep 18 '22 04:09

lambodar


The first code snippet would result in two accept-headers while the second code snippet would give one accept-header with two selectors.

They are in fact equivalent.

The spec also states that the more specific media range have precedence, so both would yield your expected behavior.

If you must specify several media ranges, and they are equally specific, you could add the q-parameter.

Source: http 1.1 spec ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html ) :

like image 31
Nils Otto Avatar answered Sep 20 '22 04:09

Nils Otto