Build query string for System.Net.HttpClient get - c#
If I wish to submit a http get request using System.Net.HttpClient there seems to be no api to add parameters, is this correct?
Is there any simple api available to build the query string that doesn't involve building a name value collection and url encoding those and then finally concatenating them?
I was hoping to use something like RestSharp's api (i.e AddParameter(..))
If I wish to submit a http get request using System.Net.HttpClient
there seems to be no api to add parameters, is this correct?
Yes.
Is there any simple api available to build the query string that
doesn't involve building a name value collection and url encoding
those and then finally concatenating them?
Sure:
var query = HttpUtility.ParseQueryString(string.Empty);
query["foo"] = "bar<>&-baz";
query["bar"] = "bazinga";
string queryString = query.ToString();
will give you the expected result:
foo=bar%3c%3e%26-baz&bar=bazinga
You might also find the UriBuilder class useful:
var builder = new UriBuilder("http://example.com");
builder.Port = -1;
var query = HttpUtility.ParseQueryString(builder.Query);
query["foo"] = "bar<>&-baz";
query["bar"] = "bazinga";
builder.Query = query.ToString();
string url = builder.ToString();
will give you the expected result:
http://example.com/?foo=bar%3c%3e%26-baz&bar=bazinga
that you could more than safely feed to your HttpClient.GetAsync method.
For those who do not want to include System.Web in projects that don't already use it, you can use FormUrlEncodedContent from System.Net.Http and do something like the following:
keyvaluepair version
string query;
using(var content = new FormUrlEncodedContent(new KeyValuePair<string, string>[]{
new KeyValuePair<string, string>("ham", "Glazed?"),
new KeyValuePair<string, string>("x-men", "Wolverine + Logan"),
new KeyValuePair<string, string>("Time", DateTime.UtcNow.ToString()),
})) {
query = content.ReadAsStringAsync().Result;
}
dictionary version
string query;
using(var content = new FormUrlEncodedContent(new Dictionary<string, string>()
{
{ "ham", "Glaced?"},
{ "x-men", "Wolverine + Logan"},
{ "Time", DateTime.UtcNow.ToString() },
})) {
query = content.ReadAsStringAsync().Result;
}
In a ASP.NET Core project you can use the QueryHelpers class, available in the Microsoft.AspNetCore.WebUtilities namespace for ASP.NET Core, or the .NET Standard 2.0 NuGet package for other consumers:
// using Microsoft.AspNetCore.WebUtilities;
var query = new Dictionary<string, string>
{
["foo"] = "bar",
["foo2"] = "bar2",
// ...
};
var response = await client.GetAsync(QueryHelpers.AddQueryString("/api/", query));
TL;DR: do not use accepted version as It's completely broken in relation to handling unicode characters, and never use internal API
I've actually found weird double encoding issue with the accepted solution:
So, If you're dealing with characters which need to be encoded, accepted solution leads to double encoding:
query parameters are auto encoded by using NameValueCollection indexer (and this uses UrlEncodeUnicode, not regular expected UrlEncode(!))
Then, when you call uriBuilder.Uri it creates new Uri using constructor which does encoding one more time (normal url encoding)
That cannot be avoided by doing uriBuilder.ToString() (even though this returns correct Uri which IMO is at least inconsistency, maybe a bug, but that's another question) and then using HttpClient method accepting string - client still creates Uri out of your passed string like this: new Uri(uri, UriKind.RelativeOrAbsolute)
Small, but full repro:
var builder = new UriBuilder
{
Scheme = Uri.UriSchemeHttps,
Port = -1,
Host = "127.0.0.1",
Path = "app"
};
NameValueCollection query = HttpUtility.ParseQueryString(builder.Query);
query["cyrillic"] = "кирилиця";
builder.Query = query.ToString();
Console.WriteLine(builder.Query); //query with cyrillic stuff UrlEncodedUnicode, and that's not what you want
var uri = builder.Uri; // creates new Uri using constructor which does encode and messes cyrillic parameter even more
Console.WriteLine(uri);
// this is still wrong:
var stringUri = builder.ToString(); // returns more 'correct' (still `UrlEncodedUnicode`, but at least once, not twice)
new HttpClient().GetStringAsync(stringUri); // this creates Uri object out of 'stringUri' so we still end up sending double encoded cyrillic text to server. Ouch!
Output:
?cyrillic=%u043a%u0438%u0440%u0438%u043b%u0438%u0446%u044f
https://127.0.0.1/app?cyrillic=%25u043a%25u0438%25u0440%25u0438%25u043b%25u0438%25u0446%25u044f
As you may see, no matter if you do uribuilder.ToString() + httpClient.GetStringAsync(string) or uriBuilder.Uri + httpClient.GetStringAsync(Uri) you end up sending double encoded parameter
Fixed example could be:
var uri = new Uri(builder.ToString(), dontEscape: true);
new HttpClient().GetStringAsync(uri);
But this uses obsolete Uri constructor
P.S on my latest .NET on Windows Server, Uri constructor with bool doc comment says "obsolete, dontEscape is always false", but actually works as expected (skips escaping)
So It looks like another bug...
And even this is plain wrong - it send UrlEncodedUnicode to server, not just UrlEncoded what server expects
Update: one more thing is, NameValueCollection actually does UrlEncodeUnicode, which is not supposed to be used anymore and is incompatible with regular url.encode/decode (see NameValueCollection to URL Query?).
So the bottom line is: never use this hack with NameValueCollection query = HttpUtility.ParseQueryString(builder.Query); as it will mess your unicode query parameters. Just build query manually and assign it to UriBuilder.Query which will do necessary encoding and then get Uri using UriBuilder.Uri.
Prime example of hurting yourself by using code which is not supposed to be used like this
You might want to check out Flurl [disclosure: I'm the author], a fluent URL builder with optional companion lib that extends it into a full-blown REST client.
var result = await "https://api.com"
// basic URL building:
.AppendPathSegment("endpoint")
.SetQueryParams(new {
api_key = ConfigurationManager.AppSettings["SomeApiKey"],
max_results = 20,
q = "Don't worry, I'll get encoded!"
})
.SetQueryParams(myDictionary)
.SetQueryParam("q", "overwrite q!")
// extensions provided by Flurl.Http:
.WithOAuthBearerToken("token")
.GetJsonAsync<TResult>();
Check out the docs for more details. The full package is available on NuGet:
PM> Install-Package Flurl.Http
or just the stand-alone URL builder:
PM> Install-Package Flurl
Along the same lines as Rostov's post, if you do not want to include a reference to System.Web in your project, you can use FormDataCollection from System.Net.Http.Formatting and do something like the following:
Using System.Net.Http.Formatting.FormDataCollection
var parameters = new Dictionary<string, string>()
{
{ "ham", "Glaced?" },
{ "x-men", "Wolverine + Logan" },
{ "Time", DateTime.UtcNow.ToString() },
};
var query = new FormDataCollection(parameters).ReadAsNameValueCollection().ToString();
Since I have to reuse this few time, I came up with this class that simply help to abstract how the query string is composed.
public class UriBuilderExt
{
private NameValueCollection collection;
private UriBuilder builder;
public UriBuilderExt(string uri)
{
builder = new UriBuilder(uri);
collection = System.Web.HttpUtility.ParseQueryString(string.Empty);
}
public void AddParameter(string key, string value) {
collection.Add(key, value);
}
public Uri Uri{
get
{
builder.Query = collection.ToString();
return builder.Uri;
}
}
}
The use will be simplify to something like this:
var builder = new UriBuilderExt("http://example.com/");
builder.AddParameter("foo", "bar<>&-baz");
builder.AddParameter("bar", "second");
var uri = builder.Uri;
that will return the uri:
http://example.com/?foo=bar%3c%3e%26-baz&bar=second
Good part of accepted answer, modified to use UriBuilder.Uri.ParseQueryString() instead of HttpUtility.ParseQueryString():
var builder = new UriBuilder("http://example.com");
var query = builder.Uri.ParseQueryString();
query["foo"] = "bar<>&-baz";
query["bar"] = "bazinga";
builder.Query = query.ToString();
string url = builder.ToString();
Darin offered an interesting and clever solution, and here is something that may be another option:
public class ParameterCollection
{
private Dictionary<string, string> _parms = new Dictionary<string, string>();
public void Add(string key, string val)
{
if (_parms.ContainsKey(key))
{
throw new InvalidOperationException(string.Format("The key {0} already exists.", key));
}
_parms.Add(key, val);
}
public override string ToString()
{
var server = HttpContext.Current.Server;
var sb = new StringBuilder();
foreach (var kvp in _parms)
{
if (sb.Length > 0) { sb.Append("&"); }
sb.AppendFormat("{0}={1}",
server.UrlEncode(kvp.Key),
server.UrlEncode(kvp.Value));
}
return sb.ToString();
}
}
and so when using it, you might do this:
var parms = new ParameterCollection();
parms.Add("key", "value");
var url = ...
url += "?" + parms;
The RFC 6570 URI Template library I'm developing is capable of performing this operation. All encoding is handled for you in accordance with that RFC. At the time of this writing, a beta release is available and the only reason it's not considered a stable 1.0 release is the documentation doesn't fully meet my expectations (see issues #17, #18, #32, #43).
You could either build a query string alone:
UriTemplate template = new UriTemplate("{?params*}");
var parameters = new Dictionary<string, string>
{
{ "param1", "value1" },
{ "param2", "value2" },
};
Uri relativeUri = template.BindByName(parameters);
Or you could build a complete URI:
UriTemplate template = new UriTemplate("path/to/item{?params*}");
var parameters = new Dictionary<string, string>
{
{ "param1", "value1" },
{ "param2", "value2" },
};
Uri baseAddress = new Uri("http://www.example.com");
Uri relativeUri = template.BindByName(baseAddress, parameters);
Or simply using my Uri extension
Code
public static Uri AttachParameters(this Uri uri, NameValueCollection parameters)
{
var stringBuilder = new StringBuilder();
string str = "?";
for (int index = 0; index < parameters.Count; ++index)
{
stringBuilder.Append(str + parameters.AllKeys[index] + "=" + parameters[index]);
str = "&";
}
return new Uri(uri + stringBuilder.ToString());
}
Usage
Uri uri = new Uri("http://www.example.com/index.php").AttachParameters(new NameValueCollection
{
{"Bill", "Gates"},
{"Steve", "Jobs"}
});
Result
http://www.example.com/index.php?Bill=Gates&Steve=Jobs
To avoid double encoding issue described in taras.roshko's answer and to keep possibility to easily work with query parameters, you can use uriBuilder.Uri.ParseQueryString() instead of HttpUtility.ParseQueryString().
Thanks to "Darin Dimitrov", This is the extension methods.
public static partial class Ext
{
public static Uri GetUriWithparameters(this Uri uri,Dictionary<string,string> queryParams = null,int port = -1)
{
var builder = new UriBuilder(uri);
builder.Port = port;
if(null != queryParams && 0 < queryParams.Count)
{
var query = HttpUtility.ParseQueryString(builder.Query);
foreach(var item in queryParams)
{
query[item.Key] = item.Value;
}
builder.Query = query.ToString();
}
return builder.Uri;
}
public static string GetUriWithparameters(string uri,Dictionary<string,string> queryParams = null,int port = -1)
{
var builder = new UriBuilder(uri);
builder.Port = port;
if(null != queryParams && 0 < queryParams.Count)
{
var query = HttpUtility.ParseQueryString(builder.Query);
foreach(var item in queryParams)
{
query[item.Key] = item.Value;
}
builder.Query = query.ToString();
}
return builder.Uri.ToString();
}
}
HttpClient client = new HttpClient();
var uri = Environment.GetEnvironmentVariable("URL of Api");
var requesturi = QueryHelpers.AddQueryString(uri, "parameter_name",parameter_value);
client.BaseAddress = new Uri(requesturi);
And then you can add request headers also eg:
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-api-key", secretValue);
response syntax eg:
HttpResponseMessage response = client.GetAsync(requesturi).Result;
Hope it will work for you.
My answer doesn't globally differ from the accepted/other answers. I just tried to create an extension method for the Uri type, which takes variable number of parameters.
public static class UriExtensions
{
public static Uri AddParameter(this Uri url, params (string Name, string Value)[] #params)
{
if (!#params.Any())
{
return url;
}
UriBuilder uriBuilder = new(url);
NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query);
foreach (var param in #params)
{
query[param.Name] = param.Value.Trim();
}
uriBuilder.Query = query.ToString();
return uriBuilder.Uri;
}
}
Usage example:
var uri = new Uri("http://someuri.com")
.AddParameter(
("p1.name", "p1.value"),
("p2.name", "p2.value"),
("p3.name", "p3.value"));
I couldn't find a better solution than creating a extension method to convert a Dictionary to QueryStringFormat. The solution proposed by Waleed A.K. is good as well.
Follow my solution:
Create the extension method:
public static class DictionaryExt
{
public static string ToQueryString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary)
{
return ToQueryString<TKey, TValue>(dictionary, "?");
}
public static string ToQueryString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, string startupDelimiter)
{
string result = string.Empty;
foreach (var item in dictionary)
{
if (string.IsNullOrEmpty(result))
result += startupDelimiter; // "?";
else
result += "&";
result += string.Format("{0}={1}", item.Key, item.Value);
}
return result;
}
}
And them:
var param = new Dictionary<string, string>
{
{ "param1", "value1" },
{ "param2", "value2" },
};
param.ToQueryString(); //By default will add (?) question mark at begining
//"?param1=value1¶m2=value2"
param.ToQueryString("&"); //Will add (&)
//"¶m1=value1¶m2=value2"
param.ToQueryString(""); //Won't add anything
//"param1=value1¶m2=value2"
Related
Sending a Get request in C# with options doesn't work [duplicate]
If I wish to submit a http get request using System.Net.HttpClient there seems to be no api to add parameters, is this correct? Is there any simple api available to build the query string that doesn't involve building a name value collection and url encoding those and then finally concatenating them? I was hoping to use something like RestSharp's api (i.e AddParameter(..))
If I wish to submit a http get request using System.Net.HttpClient there seems to be no api to add parameters, is this correct? Yes. Is there any simple api available to build the query string that doesn't involve building a name value collection and url encoding those and then finally concatenating them? Sure: var query = HttpUtility.ParseQueryString(string.Empty); query["foo"] = "bar<>&-baz"; query["bar"] = "bazinga"; string queryString = query.ToString(); will give you the expected result: foo=bar%3c%3e%26-baz&bar=bazinga You might also find the UriBuilder class useful: var builder = new UriBuilder("http://example.com"); builder.Port = -1; var query = HttpUtility.ParseQueryString(builder.Query); query["foo"] = "bar<>&-baz"; query["bar"] = "bazinga"; builder.Query = query.ToString(); string url = builder.ToString(); will give you the expected result: http://example.com/?foo=bar%3c%3e%26-baz&bar=bazinga that you could more than safely feed to your HttpClient.GetAsync method.
For those who do not want to include System.Web in projects that don't already use it, you can use FormUrlEncodedContent from System.Net.Http and do something like the following: keyvaluepair version string query; using(var content = new FormUrlEncodedContent(new KeyValuePair<string, string>[]{ new KeyValuePair<string, string>("ham", "Glazed?"), new KeyValuePair<string, string>("x-men", "Wolverine + Logan"), new KeyValuePair<string, string>("Time", DateTime.UtcNow.ToString()), })) { query = content.ReadAsStringAsync().Result; } dictionary version string query; using(var content = new FormUrlEncodedContent(new Dictionary<string, string>() { { "ham", "Glaced?"}, { "x-men", "Wolverine + Logan"}, { "Time", DateTime.UtcNow.ToString() }, })) { query = content.ReadAsStringAsync().Result; }
In a ASP.NET Core project you can use the QueryHelpers class, available in the Microsoft.AspNetCore.WebUtilities namespace for ASP.NET Core, or the .NET Standard 2.0 NuGet package for other consumers: // using Microsoft.AspNetCore.WebUtilities; var query = new Dictionary<string, string> { ["foo"] = "bar", ["foo2"] = "bar2", // ... }; var response = await client.GetAsync(QueryHelpers.AddQueryString("/api/", query));
TL;DR: do not use accepted version as It's completely broken in relation to handling unicode characters, and never use internal API I've actually found weird double encoding issue with the accepted solution: So, If you're dealing with characters which need to be encoded, accepted solution leads to double encoding: query parameters are auto encoded by using NameValueCollection indexer (and this uses UrlEncodeUnicode, not regular expected UrlEncode(!)) Then, when you call uriBuilder.Uri it creates new Uri using constructor which does encoding one more time (normal url encoding) That cannot be avoided by doing uriBuilder.ToString() (even though this returns correct Uri which IMO is at least inconsistency, maybe a bug, but that's another question) and then using HttpClient method accepting string - client still creates Uri out of your passed string like this: new Uri(uri, UriKind.RelativeOrAbsolute) Small, but full repro: var builder = new UriBuilder { Scheme = Uri.UriSchemeHttps, Port = -1, Host = "127.0.0.1", Path = "app" }; NameValueCollection query = HttpUtility.ParseQueryString(builder.Query); query["cyrillic"] = "кирилиця"; builder.Query = query.ToString(); Console.WriteLine(builder.Query); //query with cyrillic stuff UrlEncodedUnicode, and that's not what you want var uri = builder.Uri; // creates new Uri using constructor which does encode and messes cyrillic parameter even more Console.WriteLine(uri); // this is still wrong: var stringUri = builder.ToString(); // returns more 'correct' (still `UrlEncodedUnicode`, but at least once, not twice) new HttpClient().GetStringAsync(stringUri); // this creates Uri object out of 'stringUri' so we still end up sending double encoded cyrillic text to server. Ouch! Output: ?cyrillic=%u043a%u0438%u0440%u0438%u043b%u0438%u0446%u044f https://127.0.0.1/app?cyrillic=%25u043a%25u0438%25u0440%25u0438%25u043b%25u0438%25u0446%25u044f As you may see, no matter if you do uribuilder.ToString() + httpClient.GetStringAsync(string) or uriBuilder.Uri + httpClient.GetStringAsync(Uri) you end up sending double encoded parameter Fixed example could be: var uri = new Uri(builder.ToString(), dontEscape: true); new HttpClient().GetStringAsync(uri); But this uses obsolete Uri constructor P.S on my latest .NET on Windows Server, Uri constructor with bool doc comment says "obsolete, dontEscape is always false", but actually works as expected (skips escaping) So It looks like another bug... And even this is plain wrong - it send UrlEncodedUnicode to server, not just UrlEncoded what server expects Update: one more thing is, NameValueCollection actually does UrlEncodeUnicode, which is not supposed to be used anymore and is incompatible with regular url.encode/decode (see NameValueCollection to URL Query?). So the bottom line is: never use this hack with NameValueCollection query = HttpUtility.ParseQueryString(builder.Query); as it will mess your unicode query parameters. Just build query manually and assign it to UriBuilder.Query which will do necessary encoding and then get Uri using UriBuilder.Uri. Prime example of hurting yourself by using code which is not supposed to be used like this
You might want to check out Flurl [disclosure: I'm the author], a fluent URL builder with optional companion lib that extends it into a full-blown REST client. var result = await "https://api.com" // basic URL building: .AppendPathSegment("endpoint") .SetQueryParams(new { api_key = ConfigurationManager.AppSettings["SomeApiKey"], max_results = 20, q = "Don't worry, I'll get encoded!" }) .SetQueryParams(myDictionary) .SetQueryParam("q", "overwrite q!") // extensions provided by Flurl.Http: .WithOAuthBearerToken("token") .GetJsonAsync<TResult>(); Check out the docs for more details. The full package is available on NuGet: PM> Install-Package Flurl.Http or just the stand-alone URL builder: PM> Install-Package Flurl
Along the same lines as Rostov's post, if you do not want to include a reference to System.Web in your project, you can use FormDataCollection from System.Net.Http.Formatting and do something like the following: Using System.Net.Http.Formatting.FormDataCollection var parameters = new Dictionary<string, string>() { { "ham", "Glaced?" }, { "x-men", "Wolverine + Logan" }, { "Time", DateTime.UtcNow.ToString() }, }; var query = new FormDataCollection(parameters).ReadAsNameValueCollection().ToString();
Since I have to reuse this few time, I came up with this class that simply help to abstract how the query string is composed. public class UriBuilderExt { private NameValueCollection collection; private UriBuilder builder; public UriBuilderExt(string uri) { builder = new UriBuilder(uri); collection = System.Web.HttpUtility.ParseQueryString(string.Empty); } public void AddParameter(string key, string value) { collection.Add(key, value); } public Uri Uri{ get { builder.Query = collection.ToString(); return builder.Uri; } } } The use will be simplify to something like this: var builder = new UriBuilderExt("http://example.com/"); builder.AddParameter("foo", "bar<>&-baz"); builder.AddParameter("bar", "second"); var uri = builder.Uri; that will return the uri: http://example.com/?foo=bar%3c%3e%26-baz&bar=second
Good part of accepted answer, modified to use UriBuilder.Uri.ParseQueryString() instead of HttpUtility.ParseQueryString(): var builder = new UriBuilder("http://example.com"); var query = builder.Uri.ParseQueryString(); query["foo"] = "bar<>&-baz"; query["bar"] = "bazinga"; builder.Query = query.ToString(); string url = builder.ToString();
Darin offered an interesting and clever solution, and here is something that may be another option: public class ParameterCollection { private Dictionary<string, string> _parms = new Dictionary<string, string>(); public void Add(string key, string val) { if (_parms.ContainsKey(key)) { throw new InvalidOperationException(string.Format("The key {0} already exists.", key)); } _parms.Add(key, val); } public override string ToString() { var server = HttpContext.Current.Server; var sb = new StringBuilder(); foreach (var kvp in _parms) { if (sb.Length > 0) { sb.Append("&"); } sb.AppendFormat("{0}={1}", server.UrlEncode(kvp.Key), server.UrlEncode(kvp.Value)); } return sb.ToString(); } } and so when using it, you might do this: var parms = new ParameterCollection(); parms.Add("key", "value"); var url = ... url += "?" + parms;
The RFC 6570 URI Template library I'm developing is capable of performing this operation. All encoding is handled for you in accordance with that RFC. At the time of this writing, a beta release is available and the only reason it's not considered a stable 1.0 release is the documentation doesn't fully meet my expectations (see issues #17, #18, #32, #43). You could either build a query string alone: UriTemplate template = new UriTemplate("{?params*}"); var parameters = new Dictionary<string, string> { { "param1", "value1" }, { "param2", "value2" }, }; Uri relativeUri = template.BindByName(parameters); Or you could build a complete URI: UriTemplate template = new UriTemplate("path/to/item{?params*}"); var parameters = new Dictionary<string, string> { { "param1", "value1" }, { "param2", "value2" }, }; Uri baseAddress = new Uri("http://www.example.com"); Uri relativeUri = template.BindByName(baseAddress, parameters);
Or simply using my Uri extension Code public static Uri AttachParameters(this Uri uri, NameValueCollection parameters) { var stringBuilder = new StringBuilder(); string str = "?"; for (int index = 0; index < parameters.Count; ++index) { stringBuilder.Append(str + parameters.AllKeys[index] + "=" + parameters[index]); str = "&"; } return new Uri(uri + stringBuilder.ToString()); } Usage Uri uri = new Uri("http://www.example.com/index.php").AttachParameters(new NameValueCollection { {"Bill", "Gates"}, {"Steve", "Jobs"} }); Result http://www.example.com/index.php?Bill=Gates&Steve=Jobs
To avoid double encoding issue described in taras.roshko's answer and to keep possibility to easily work with query parameters, you can use uriBuilder.Uri.ParseQueryString() instead of HttpUtility.ParseQueryString().
Thanks to "Darin Dimitrov", This is the extension methods. public static partial class Ext { public static Uri GetUriWithparameters(this Uri uri,Dictionary<string,string> queryParams = null,int port = -1) { var builder = new UriBuilder(uri); builder.Port = port; if(null != queryParams && 0 < queryParams.Count) { var query = HttpUtility.ParseQueryString(builder.Query); foreach(var item in queryParams) { query[item.Key] = item.Value; } builder.Query = query.ToString(); } return builder.Uri; } public static string GetUriWithparameters(string uri,Dictionary<string,string> queryParams = null,int port = -1) { var builder = new UriBuilder(uri); builder.Port = port; if(null != queryParams && 0 < queryParams.Count) { var query = HttpUtility.ParseQueryString(builder.Query); foreach(var item in queryParams) { query[item.Key] = item.Value; } builder.Query = query.ToString(); } return builder.Uri.ToString(); } }
HttpClient client = new HttpClient(); var uri = Environment.GetEnvironmentVariable("URL of Api"); var requesturi = QueryHelpers.AddQueryString(uri, "parameter_name",parameter_value); client.BaseAddress = new Uri(requesturi); And then you can add request headers also eg: client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("x-api-key", secretValue); response syntax eg: HttpResponseMessage response = client.GetAsync(requesturi).Result; Hope it will work for you.
My answer doesn't globally differ from the accepted/other answers. I just tried to create an extension method for the Uri type, which takes variable number of parameters. public static class UriExtensions { public static Uri AddParameter(this Uri url, params (string Name, string Value)[] #params) { if (!#params.Any()) { return url; } UriBuilder uriBuilder = new(url); NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query); foreach (var param in #params) { query[param.Name] = param.Value.Trim(); } uriBuilder.Query = query.ToString(); return uriBuilder.Uri; } } Usage example: var uri = new Uri("http://someuri.com") .AddParameter( ("p1.name", "p1.value"), ("p2.name", "p2.value"), ("p3.name", "p3.value"));
I couldn't find a better solution than creating a extension method to convert a Dictionary to QueryStringFormat. The solution proposed by Waleed A.K. is good as well. Follow my solution: Create the extension method: public static class DictionaryExt { public static string ToQueryString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary) { return ToQueryString<TKey, TValue>(dictionary, "?"); } public static string ToQueryString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, string startupDelimiter) { string result = string.Empty; foreach (var item in dictionary) { if (string.IsNullOrEmpty(result)) result += startupDelimiter; // "?"; else result += "&"; result += string.Format("{0}={1}", item.Key, item.Value); } return result; } } And them: var param = new Dictionary<string, string> { { "param1", "value1" }, { "param2", "value2" }, }; param.ToQueryString(); //By default will add (?) question mark at begining //"?param1=value1¶m2=value2" param.ToQueryString("&"); //Will add (&) //"¶m1=value1¶m2=value2" param.ToQueryString(""); //Won't add anything //"param1=value1¶m2=value2"
C# .net core 3.1 accessing to Json elements
I have below code that works, and the response is Json data as below: { "code": 0, "data": { "BCH": { # BCH account "available": "13.60109", # Available BCH "frozen": "0.00000" # Frozen BCH }, "BTC": { # BTC account "available": "32590.16", # Available BTC "frozen": "7000.00" # Frozen BTC }, "ETH": { # ETH account "available": "5.06000", # Available ETH "frozen": "0.00000" # Frozen ETH } }, "message": "Ok" } How can i access to Json elements in this code? adding Json data to array or variable, i read other sample codes in stackoverflow but i coudn't do. private JsonElement Post(string path, Dictionary<string, object> args, string signature = null) { var json = JsonSerializer.Serialize(args); var content = new StringContent(json); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var req = new HttpRequestMessage(HttpMethod.Post, this.baseUrl + path); req.Content = content; if (signature != null) { req.Headers.Add("authorization", signature); } var res = this.client.SendAsync(req).Result; var result = res.Content.ReadAsStringAsync().Result; return JsonSerializer.Deserialize<JsonElement>(result); } private JsonElement Get(string path, Dictionary<string, object> args = null, string signature = null) { var url = this.baseUrl + path; if (args != null) { var param = string.Join("&", args.Select(p => $"{p.Key}={p.Value}")); url = url + "?" + param; } string result; if (signature != null) { var req = new HttpRequestMessage(HttpMethod.Get, url); req.Headers.Add("authorization", signature); var res = client.SendAsync(req).Result; result = res.Content.ReadAsStringAsync().Result; } else { result = this.client.GetStringAsync(url).Result; } return JsonSerializer.Deserialize<JsonElement>(result); }
I'm not quite sure if I understand your question correctly, however if I understand you're trying to access values inside the JsonElement type you have deserialized the response to? If so, you can lean on the GetProperty and TryGetProperty methods, and the Item[Int32] (if the object is an array) property of the JsonElement struct. For example myJsonElement.GetProperty("data").GetProperty("BCH").GetProperty("available").ToString() would return the string "13.60109" from your example JSON. However this isn't particularly practical as it could quickly get messy if you want to extract more than one or two values. As Jonathan Alfaro has suggested your best bet would be to define a strongly typed model and deserialize to that type instead.
Building a List with multiple URL Params in C#
I am trying to build a generic function that will run a simple HTTP Get request using various possible URL Params. I want to be able to receive a flexible number of strings as a parameter and add them one by one as a URL parameter in the request. Here's my code so far, I am trying to build a List but for some reason I just can't muster a workign solution.. public static void GetRequest(List<string> lParams) { lParams.Add(header1); string myURL = ""; HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format(myURL)); WebReq.Method = "GET"; HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); Stream Answer = WebResp.GetResponseStream(); StreamReader _Answer = new StreamReader(Answer); sContent = _Answer.ReadToEnd(); } Thanks!
I think you need this: private static string CreateUrl(string baseUrl, Dictionary<string, string> args) { var sb = new StringBuilder(baseUrl); var f = true; foreach (var arg in args) { sb.Append(f ? '?' : '&'); sb.Append(WebUtility.UrlEncode(arg.Key) + '=' + WebUtility.UrlEncode(arg.Value)); f = false; } return sb.ToString(); } Not so complex version with comments: private static string CreateUrl(string baseUrl, Dictionary<string, string> parameters) { var stringBuilder = new StringBuilder(baseUrl); var firstTime = true; // Going through all the parameters foreach (var arg in parameters) { if (firstTime) { stringBuilder.Append('?'); // first parameter is appended with a ? - www.example.com/index.html?abc=3 firstTime = false; // All other following parameters should be added with a & } else { stringBuilder.Append('&'); // all other parameters are appended with a & - www.example.com/index.html?abc=3&abcd=4&abcde=8 } var key = WebUtility.UrlEncode(arg.Key); // Converting characters which are not allowed in the url to escaped values var value = WebUtility.UrlEncode(arg.Value); // Same goes for the value stringBuilder.Append(key + '=' + value); // Writing the parameter in the format key=value } return stringBuilder.ToString(); // Returning the url with parameters }
Synchronous VirusTotal code to Rx enabled asynchronous (and maybe some JSON parsing)
last time I posted a question on here everyone provided some great guidance on getting my problem solved. Move forward in time and here is another. I'm attempting to redo a small helper tool I have that checks URL's and Files against VirusTotal to get some basic information. The code below works quite well but locks up the UI. I was told that I should look into Rx and am enjoying reading up on it but cannot seem to get my head wrapped around it. So now here is where the question comes in, what is the best way to design the following code to make it utilize Rx so that it is asynchronous and leaves my UI alone while it does it's thing. VirusTotal also utilizes multilevel JSON for responses so if anyone has a nice way of integrating that into this that would even be better. class Virustotal { private string APIKey = "REMOVED"; private string FileReportURL = "https://www.virustotal.com/vtapi/v2/file/report"; private string URLReportURL = "http://www.virustotal.com/vtapi/v2/url/report"; private string URLSubmitURL = "https://www.virustotal.com/vtapi/v2/url/scan"; WebRequest theRequest; HttpWebResponse theResponse; ArrayList theQueryData; public string GetFileReport(string checksum) // Gets latest report of file from VT using a hash (MD5 / SHA1 / SHA256) { this.WebPostRequest(this.FileReportURL); this.Add("resource", checksum); return this.GetResponse(); } public string GetURLReport(string url) // Gets latest report of URL from VT { this.WebPostRequest(this.URLReportURL); this.Add("resource", url); this.Add("scan", "1"); //Automatically submits to VT if no result found return this.GetResponse(); } public string SubmitURL(string url) // Submits URL to VT for insertion to scanning queue { this.WebPostRequest(this.URLSubmitURL); this.Add("url", url); return this.GetResponse(); } public string SubmitFile() // Submits File to VT for insertion to scanning queue { // File Upload code needed return this.GetResponse(); } private void WebPostRequest(string url) { theRequest = WebRequest.Create(url); theRequest.Method = "POST"; theQueryData = new ArrayList(); this.Add("apikey", APIKey); } private void Add(string key, string value) { theQueryData.Add(String.Format("{0}={1}", key, Uri.EscapeDataString(value))); } private string GetResponse() { // Set the encoding type theRequest.ContentType="application/x-www-form-urlencoded"; // Build a string containing all the parameters string Parameters = String.Join("&",(String[]) theQueryData.ToArray(typeof(string))); theRequest.ContentLength = Parameters.Length; // We write the parameters into the request StreamWriter sw = new StreamWriter(theRequest.GetRequestStream()); sw.Write(Parameters); sw.Close(); // Execute the query theResponse = (HttpWebResponse)theRequest.GetResponse(); StreamReader sr = new StreamReader(theResponse.GetResponseStream()); return sr.ReadToEnd(); } }
Your code is poorly written which makes it more difficult to make it asynchronous - primarily the three class-level variables. When coding in Rx you want to think "functional programming" and not "OOP" - so no class-level variables. So, what I've done is this - I've recoded the GetResponse method to encapsulate all of the state into a single call - and I've made it return IObservable<string> rather than just string. The public functions can now be written like this: public IObservable<string> GetFileReport(string checksum) { return this.GetResponse(this.FileReportURL, new Dictionary<string, string>() { { "resource", checksum }, }); } public IObservable<string> GetURLReport(string url) { return this.GetResponse(this.URLReportURL, new Dictionary<string, string>() { { "resource", url }, { "scan", "1" }, }); } public IObservable<string> SubmitURL(string url) { return this.GetResponse(this.URLSubmitURL, new Dictionary<string, string>() { { "url", url }, }); } public IObservable<string> SubmitFile() { return this.GetResponse("UNKNOWNURL", new Dictionary<string, string>()); } And GetResponse looks like this: private IObservable<string> GetResponse( string url, Dictionary<string, string> theQueryData) { return Observable.Start(() => { var theRequest = WebRequest.Create(url); theRequest.Method = "POST"; theRequest.ContentType="application/x-www-form-urlencoded"; theQueryData.Add("apikey", APIKey); string Parameters = String.Join("&", theQueryData.Select(x => String.Format("{0}={1}", x.Key, x.Value))); theRequest.ContentLength = Parameters.Length; using (var sw = new StreamWriter(theRequest.GetRequestStream())) { sw.Write(Parameters); sw.Close(); } using (var theResponse = (HttpWebResponse)theRequest.GetResponse()) { using (var sr = new StreamReader(theResponse.GetResponseStream())) { return sr.ReadToEnd(); } } }); } I haven't actually tested this - I don't have the APIKEY for starters - but it should work OK. Let me know how you go.
Replace item in querystring
I have a URL that also might have a query string part, the query string might be empty or have multiple items. I want to replace one of the items in the query string or add it if the item doesn't already exists. I have an URI object with the complete URL. My first idea was to use regex and some string magic, that should do it. But it seems a bit shaky, perhaps the framework has some query string builder class?
I found this was a more elegant solution var qs = HttpUtility.ParseQueryString(Request.QueryString.ToString()); qs.Set("item", newItemValue); Console.WriteLine(qs.ToString());
Lets have this url: https://localhost/video?param1=value1 At first update specific query string param to new value: var uri = new Uri("https://localhost/video?param1=value1"); var qs = HttpUtility.ParseQueryString(uri.Query); qs.Set("param1", "newValue2"); Next create UriBuilder and update Query property to produce new uri with changed param value. var uriBuilder = new UriBuilder(uri); uriBuilder.Query = qs.ToString(); var newUri = uriBuilder.Uri; Now you have in newUri this value: https://localhost/video?param1=newValue2
Maybe you could use the System.UriBuilder class. It has a Query property.
I use following method: public static string replaceQueryString(System.Web.HttpRequest request, string key, string value) { System.Collections.Specialized.NameValueCollection t = HttpUtility.ParseQueryString(request.Url.Query); t.Set(key, value); return t.ToString(); }
string link = page.Request.Url.ToString(); if(page.Request.Url.Query == "") link += "?pageIndex=" + pageIndex; else if (page.Request.QueryString["pageIndex"] != "") { var idx = page.Request.QueryString["pageIndex"]; link = link.Replace("pageIndex=" + idx, "pageIndex=" + pageIndex); } else link += "&pageIndex=" + pageIndex; This seems to work really well.
No, the framework doesn't have any existing QueryStringBuilder class, but usually the querystring information in a HTTP request is available as an iterable and searchable NameValueCollection via the Request.Querystring property. Since you are starting off with a Uri object, however, you will need to obtain the querystring portion using the Query property of the Uri object. This will yield a string of the form: Uri myURI = new Uri("http://www.mywebsite.com/page.aspx?Val1=A&Val2=B&Val3=C"); string querystring = myURI.Query; // Outputs: "?Val1=A&Val2=B&Val3=C". Note the ? prefix! Console.WriteLine(querystring); You can then split this string on the ampersand character to differentiate it into different querystring parameters-value pairs. Then again split each parameter on the "=" character to differentiate it into a key and value. Since your final goal is to search for a particular querystring key and if necessary create it, you should try to (re)create a collection (preferably, a generic one) that allows you easily search in the collection, similar to the facility provided by the NameValueCollection class.
I used the following code to append/replace the value of a parameter in the current request URL: public static string CurrentUrlWithParam(this UrlHelper helper, string paramName, string paramValue) { var url = helper.RequestContext.HttpContext.Request.Url; var sb = new StringBuilder(); sb.AppendFormat("{0}://{1}{2}{3}", url.Scheme, url.Host, url.IsDefaultPort ? "" : ":" + url.Port, url.LocalPath); var isFirst = true; if (!String.IsNullOrWhiteSpace(url.Query)) { var queryStrings = url.Query.Split(new[] { '?', ';' }); foreach (var queryString in queryStrings) { if (!String.IsNullOrWhiteSpace(queryString) && !queryString.StartsWith(paramName + "=")) { sb.AppendFormat("{0}{1}", isFirst ? "?" : ";", queryString); isFirst = false; } } } sb.AppendFormat("{0}{1}={2}", isFirst ? "?" : ";", paramName, paramValue); return sb.ToString(); } Maybe this helps others when finding this topic. Update: Just saw the hint about UriBuilder and did a second version using UriBuilder, StringBuilder and Linq: public static string CurrentUrlWithParam(this UrlHelper helper, string paramName, string paramValue) { var url = helper.RequestContext.HttpContext.Request.Url; var ub = new UriBuilder(url.Scheme, url.Host, url.Port, url.LocalPath); // Query string var sb = new StringBuilder(); var isFirst = true; if (!String.IsNullOrWhiteSpace(url.Query)) { var queryStrings = url.Query.Split(new[] { '?', ';' }); foreach (var queryString in queryStrings.Where(queryString => !String.IsNullOrWhiteSpace(queryString) && !queryString.StartsWith(paramName + "="))) { sb.AppendFormat("{0}{1}", isFirst ? "" : ";", queryString); isFirst = false; } } sb.AppendFormat("{0}{1}={2}", isFirst ? "" : ";", paramName, paramValue); ub.Query = sb.ToString(); return ub.ToString(); }
I agree with Cerebrus. Sticking to the KISS principle, you have the querystring, string querystring = myURI.Query; you know what you are looking for and what you want to replace it with. So use something like this:- if (querystring == "") myURI.Query += "?" + replacestring; else querystring.replace (searchstring, replacestring); // not too sure of syntax !!
I answered a similar question a while ago. Basically, the best way would be to use the class HttpValueCollection, which the QueryString property actually is, unfortunately it is internal in the .NET framework. You could use Reflector to grab it (and place it into your Utils class). This way you could manipulate the query string like a NameValueCollection, but with all the url encoding/decoding issues taken care for you. HttpValueCollection extends NameValueCollection, and has a constructor that takes an encoded query string (ampersands and question marks included), and it overrides a ToString() method to later rebuild the query string from the underlying collection.
public class QueryParams : Dictionary<string,string> { private Uri originolUrl; private Uri ammendedUrl; private string schemeName; private string hostname; private string path; public QueryParams(Uri url) { this.originolUrl = url; schemeName = url.Scheme; hostname = url.Host; path = url.AbsolutePath; //check uri to see if it has a query if (url.Query.Count() > 1) { //we grab the query and strip of the question mark as we do not want it attached string query = url.Query.TrimStart("?".ToArray()); //we grab each query and place them into an array string[] parms = query.Split("&".ToArray()); foreach (string str in parms) { // we split each query into two strings(key) and (value) and place into array string[] param = str.Split("=".ToArray()); //we add the strings to this dictionary this.Add(param[0], param[1]); } } } public QueryParams Set(string paramName, string value) { if(this.ContainsKey(paramName)) { //if key exists change value this[paramName] = value; return (this); } else { this.Add(paramName, value); return this; } } public QueryParams Set(string paramName, int value) { if (this.ContainsKey(paramName)) { //if key exists change value this[paramName] = value.ToString(); return (this); } else { this.Add(paramName, value); return this; } } public void Add(string key, int value) { //overload, adds a new keypair string strValue = value.ToString(); this.Add(key, strValue); } public override string ToString() { StringBuilder queryString = new StringBuilder(); foreach (KeyValuePair<string, string> pair in this) { //we recreate the query from each keypair queryString.Append(pair.Key + "=" + pair.Value + "&"); } //trim the end of the query string modifiedQuery = queryString.ToString().TrimEnd("&".ToArray()); if (this.Count() > 0) { UriBuilder uriBuild = new UriBuilder(schemeName, hostname); uriBuild.Path = path; uriBuild.Query = modifiedQuery; ammendedUrl = uriBuild.Uri; return ammendedUrl.AbsoluteUri; } else { return originolUrl.ToString(); } } public Uri ToUri() { this.ToString(); return ammendedUrl; } } }