I was going through the HttpURLConnection API doc and found two methods
getHeaderFields () Which Returns an un-modifiable map of the response-header fields and values
and
getRequestProperties () which also Returns an un-modifiable map of general request properties used by this connection.
I want to know what is difference between this two methods?
From My understanding both methods are used to store key,value pair in HTTP Header.
Correct me if I am wrong.
Thanks in advance.
getHeaderFields() returns all the fields and values while getRequestProperties(String field) returns the properties of the requested fields (not all).
From < Android API 22 Platform > javaDoc:
public Map<...> getHeaderFields ()
Added in API level 1
Returns an unmodifiable map of the response-header fields and values. The response-header field names are the key values of the map. The map values are lists of header field values associated with a particular key name.
Some implementations (notably HttpURLConnection) include a mapping for the null key; in HTTP's case, this maps to the HTTP status line and is treated as being at position 0 when indexing into the header fields.
Returns
the response-header representing generic map
And for request
public Map<...> getRequestProperties ()
Added in API level 1
Returns an unmodifiable map of general request properties used by this connection. The request property names are the key values of the map. The map values are lists of property values of the corresponding key name.
Returns
the request-property representing generic map.
Related
I have Map variable in my Freemarker template. How can I fetch a value at a particular key in the map, as we do in Java (map.get(<key>)).
I know how to iterate through keys and values of a map in a FTL. But I want a solution without iteration, on the lines of Java get() method of Map interface.
map[dynamicKey] or map.staticKey if the key is a string. Due to historical limitations, if the key isn't a string, map?api.get(nonStringKey).
I'm creating a endpoint (using Jersey annotations) that receives a list of Strings as it's parameter. The strings need to contain a key and a value.
So on the url it would look something like
?params=key1=value1¶ms=key2=value2¶ms....
Two questions:
I'm not sure if the above example would work, I assume you would have to encode the = sign between key and value. Is that correct?
Is there a better or more standard way to approach this?
Obviously the most standard way would be to have the RHS be the key and the LHS of the equal sign would be the value. The problem is I don't want to define every possible key value pair in my method signature. I want to accept a general list of keys and values and let the user pass in the pairs that are relevant to their query.
So far I've tried several google queries but haven't found an answer:
java url key and value parameter
java url parameters with key and value
list of key value pairs in url java
Storing a list of key value pairs in a url parameter, javascript. (has an answer for php but I'm using Java, would this work in Java?)
how to send an array in url request (suggests the above would work in java, if so, can the index be a string like ¶ms[key2]=value2...?)
Yes. Jersey can handle it even = between key and value is not encoded.If query parameter is params=key1=value1¶ms=key2=value2 , the params list below will be [key1=value1, key2=value2]
#GET
public Response q34211549(#QueryParam("params") List<String> params) {
//parse params list and convert it into key and value
}
You can also consider to create a ParamConverter to let Jersey to do this conversion for you.
Reference
JAX-RS ParamConverter and ParamConverterProvider Example
How can i add multiple values with the same name in a HttpUrlConnection request.
example:
HashMap<String, String> params = new HashMap<>();
params.put("key[]", value1)
params.put("key[]", value2)
If i try to add multiple values with the same in postman i works fine, the application will send only one values (depends on request property, URLConnection setRequestProperty vs addRequestProperty).
I want to add both values as a parameter with the same name
This is not possible with Maps or HashMaps.
Taken from Oracles documentation on Maps:
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
The put command will replace the previous value associated with the given key in the map (you can think of this like an array indexing operation for primitive types).
The Oracle Documentation for put states:
Associates the specified value with the specified key in this map. If
the map previously contained a mapping for the key, the old value is
replaced.
Returns the previous value associated with key, or null if there was
no mapping for key.
This can be found here:
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#put%28K,%20V%29
Alternatively You can do this and it will work fine.
You can make a JSONArray like this
JSONArray array = new JSONArray();
array.put("value1");
array.put("value2");
//and then you can send them as parameter like this-
params.put("key", array.toString());
It is not possible with params.put() But it is possible with params.add()
Reference : Difference between RequestParams add() and put() in AndroidAsyncHttp
There's getHeaderFields() but that returns the headers in the response HTTP message. I want the headers in the request message, not the response.
Also, what is the difference between Properties and Headers? It seems like setRequestProperty() is setting a header, but I'm not sure.
If you check the method getRequestProperties() you'll find your guess is right:
public Map<String,List<String>> getRequestProperties()
Returns an unmodifiable Map of general request properties for this
connection. The Map keys are Strings that represent the request-header
field names. Each Map value is a unmodifiable List of Strings that
represents the corresponding field values.
Well OK, I got confused. I believe it returns the pointer to the original map?
private HttpServletRequest originalRequest;
Map params = originalRequest.getParameterMap();
params.remove("parameter-to-remove");
params.put("parameter-to-add", "<a value>");
Now are the parameters in the originalRequest going to change after these actions? Or does it just copy the values to params and it doesn't matter what I do with them and nothings going to be changed in originalRequest?
Returned map is immutable Map, that could be the reason why you are not seeing the changes reflected.
As per getParameterMap javadoc
an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array.
If you would like to set some value to request, you should use setAttribute.
No, you are not allowed to remove or add any request parameter(s) to the request object. They must remain (as they arrived to the server) until the request object goes out of scope (after the end of the request processing cycle).
Logically, if you were allowed to do something like that, then the request object would not represent the original request any more. During the whole request processing cycle, we want to process the request sent by the client, not the one that has been tampered.
The method you should use instead is void setAttribute(java.lang.String name, java.lang.Object o).