[英]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.