简体   繁体   English

在两个asp.net MVC Web应用程序之间传递非ASCII字符将无法识别

[英]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 :- 我有两个asp.net mvc-4和mvc-5 web应用程序,现在在我的第一个asp.net mvc中,我有以下WebClient在第二个Web应用程序上调用一个动作方法(Home / CreateResource): -

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 .. 现在在传输到第二个操作方法的data对象中,它包含一个密码值,在我的情况下,这个值是££123 ,有2个非ASCII字符。

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 ? 如果有办法在2个动作方法之间传递非ASCII字符,那么任何人都可以这样做吗? 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 £. 如果要传输非ASCII(或高ASCII)字符(例如£),则需要指定WebClient的Encoding属性。

The order in which to set the property values is not important in this case, provided it is set before calling UploadString . 在这种情况下,设置属性值的顺序并不重要,只要在调用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. 如果您希望使用WebClient从网站下载数据,请确保将编码设置为该网站使用的值。

simply change wc.Headers.Add(HttpRequestHeader.ContentType, "application/json"); 只需更改wc.Headers.Add(HttpRequestHeader.ContentType, "application/json"); to wc.Headers.Add(HttpRequestHeader.ContentType, "application/json;charset=utf-8"); 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 解决方案是使用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: 虽然理论上UTF-8应该能够编码你需要的任何字符(因为它有多字节编码方案来处理扩展的字符),但另一种解决方案是:

Base 64 encode your text. Base 64对您的文本进行编码。

To encode s : 编码s

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

To decode encoded : 解码encoded

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET Core中编写响应时,非ASCII字符会加扰 - Non-ASCII characters scrambled when writing response in ASP.NET Core 在 ASP.NET 核心 API 2.1 中发送请求,其中 header 带有非 ASCII 字符 - Sending a request in ASP.NET Core API 2.1 with a header with non-ASCII characters .Net MVC 2,返回文件名中包含非ASCII字符的文件 - .Net MVC 2, return File that contains non-ASCII characters in the filename ASP.NET MVC 2中的非ASCII字符 - Non ASCII character in ASP.NET MVC 2 在两个ASP.NET MVC应用程序之间共享Cookie - Sharing cookie between two asp.net mvc applications ASP.NET MVC-两个应用程序之间的相同身份验证 - ASP.NET MVC - Same authentication between two applications 具有非ASCII字符的AWS开发工具包.NET自定义元数据 - AWS SDK .NET custom metadata with non-ASCII characters 如何在ASP.NET MVC 4 Web应用程序中使用LINQ更新两个表 - How to Update Two Tables using LINQ in ASP.NET MVC 4 Web Applications 在Web应用程序和Visual Studio for ASP.NET MVC上使用图像 - Using images on Web Applications and Visual Studio for ASP.NET MVC 身份:在两个不同的asp.net mvc5应用程序中的身份验证 - Identity: Authentication in Two Different asp.net mvc5 Applications
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM