How to know if an HTTP request header value exists

if (Request.Headers[“XYZComponent”].Count() > 0) … will attempted to count the number of characters in the returned string, but if the header doesn’t exist it will return NULL, hence why it’s throwing an exception. Your second example effectively does the same thing, it will search through the collection of Headers and return NULL if it doesn’t … Read more

Getting a Request.Headers value

if (Request.Headers[“XYZComponent”].Count() > 0) … will attempted to count the number of characters in the returned string, but if the header doesn’t exist it will return NULL, hence why it’s throwing an exception. Your second example effectively does the same thing, it will search through the collection of Headers and return NULL if it doesn’t … Read more

NameValueCollection to URL Query?

Simply calling ToString() on the NameValueCollection will return the name value pairs in a name1=value1&name2=value2 querystring ready format. Note that NameValueCollection types don’t actually support this and it’s misleading to suggest this, but the behavior works here due to the internal type that’s actually returned, as explained below. Thanks to @mjwills for pointing out that … Read more

how to convert NameValueCollection to JSON string?

One way to serialize NameValueCollection is by first converting it to Dictionary and then serialize the Dictionary. To convert to dictionary: thenvc.AllKeys.ToDictionary(k => k, k => thenvc[k]); If you need to do the conversion frequently, you can also create an extension method to NameValueCollection: public static class NVCExtender { public static IDictionary<string, string> ToDictionary( this … Read more

Parse a URI String into Name-Value Collection

If you are looking for a way to achieve it without using an external library, the following code will help you. public static Map<String, String> splitQuery(URL url) throws UnsupportedEncodingException { Map<String, String> query_pairs = new LinkedHashMap<String, String>(); String query = url.getQuery(); String[] pairs = query.split(“&”); for (String pair : pairs) { int idx = pair.indexOf(“=”); … Read more