简体   繁体   中英

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

在此输入图像描述

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));
}

Although UTF-8 theoretically should be capable of encoding any characters you need (as it has a muti-byte encoding scheme to handle extended chracters), an alternative solution:

Base 64 encode your text.

To encode s :

string encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(s));

To decode encoded :

string s = Encoding.ASCII.GetString(Convert.FromBase64String(encoded));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM