Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing non-ASCII characters between two asp.net MVC web applications will not be recognized

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:-

enter image description here

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??

like image 392
john Gu Avatar asked Aug 01 '16 11:08

john Gu


3 Answers

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.

like image 73
LocEngineer Avatar answered Nov 18 '22 20:11

LocEngineer


simply change wc.Headers.Add(HttpRequestHeader.ContentType, "application/json"); to wc.Headers.Add(HttpRequestHeader.ContentType, "application/json;charset=utf-8");

like image 22
Suyash Kumar Singh Avatar answered Nov 18 '22 20:11

Suyash Kumar Singh


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));
}
like image 1
VDWWD Avatar answered Nov 18 '22 20:11

VDWWD