I have two asp.net mvc-4 and mvc-5 web applications,, now inside my first asp.net mvc i have the following WebClient
to call an action method (Home/CreateResource) on the second web application :-
using (WebClient wc = new WebClient())
{
var data = JsonConvert.SerializeObject(cr);
string url = scanningurl + "Home/CreateResource";
Uri uri = new Uri(url);
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
wc.Headers.Add("Authorization", token);
output = wc.UploadString(uri, data);
}
now inside the data
object which is being transferred to the second action method, it contain a value for password and this value in my case is ££123
which have 2 non-ASCII characters ..
now on the second action method it will accept the above value as follow:-
so can anyone adivce if there is a way to pass non-ASCII characters between the 2 action methods ? i check that on the first action method the password is being Serialized well , and also the password is being passed correctly from the view to the action method. but the problem is somewhere inside how the data is being transferred inside the network or how the model binder on the second action method is accepting the incoming data object??
To summarize:
You need to specify the WebClient's Encoding property if you want to transmit non-ASCII (or high-ASCII) characters such as £.
The order in which to set the property values is not important in this case, provided it is set before calling UploadString
.
using (WebClient wc = new WebClient())
{
var data = JsonConvert.SerializeObject(cr);
string url = scanningurl + "Home/CreateResource";
Uri uri = new Uri(url);
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
wc.Headers.Add("Authorization", token);
wc.Encoding = Encoding.UTF8;
output = wc.UploadString(uri, data);
}
In case you wish to download data from a website using WebClient, make sure to set the encoding to the value used by that website.
simply change wc.Headers.Add(HttpRequestHeader.ContentType, "application/json");
to wc.Headers.Add(HttpRequestHeader.ContentType, "application/json;charset=utf-8");
I had a similar problem a while back. The solution was to use UploadDataAsync
using (WebClient client = new WebClient())
{
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.Headers.Add(HttpRequestHeader.Accept, "application/json");
client.UploadDataAsync(new Uri(apiUrl), "POST", Encoding.UTF8.GetBytes(jsonString));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With