用于WebClient.UploadValues的UTF32?

我在这里使用webclient在post-request中上传值:

using (var client = new WebClient()) { client.Encoding = Encoding.UTF32; var parameters = new NameValueCollection { {"facebookID", IngamableCommunicator.FacebookProfileID + ""}, {"name", name}, {"content", File.ReadAllText(mapPath)} }; client.UploadValues(url, parameters); } 

您可以清楚地看到,我认为这是如何将请求内容编码更改为UTF32。 我对吗? 因为显然,它没有按预期工作。

WebClient.Encoding属性备注:

UploadStringUploadStringAsync方法使用此属性在上载字符串之前将指定的字符串转换为Byte数组。 有关其他信息,请参阅GetBytes方法。

使用DownloadStringDownloadStringAsync方法DownloadString字符串时,WebClient使用此返回的Encoding将下载的Byte数组转换为字符串。 有关其他信息,请参阅GetString方法。

但没有关于UploadValues编码…

您不能将UploadValues方法用于非Ascii编码,即构建请求的内部方法:

 // System.Net.WebClient private byte[] UploadValuesInternal(NameValueCollection data) { if (this.m_headers == null) { this.m_headers = new WebHeaderCollection(WebHeaderCollectionType.WebRequest); } string text = this.m_headers["Content-Type"]; if (text != null && string.Compare(text, "application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase) != 0) { throw new WebException(SR.GetString("net_webclient_ContentType")); } this.m_headers["Content-Type"] = "application/x-www-form-urlencoded"; string value = string.Empty; StringBuilder stringBuilder = new StringBuilder(); string[] allKeys = data.AllKeys; for (int i = 0; i < allKeys.Length; i++) { string text2 = allKeys[i]; stringBuilder.Append(value); stringBuilder.Append(WebClient.UrlEncode(text2)); stringBuilder.Append("="); stringBuilder.Append(WebClient.UrlEncode(data[text2])); value = "&"; } byte[] bytes = Encoding.ASCII.GetBytes(stringBuilder.ToString()); this.m_ContentLength = (long)bytes.Length; return bytes; } 

因此,它使用Encoding.ASCII或WebClient.UrlEncode而不设置编码

使用HttpWebRequest。

只需将服务器端收到的POST内容从Url格式化为普通字符串:

 var postValues= WebUtility.UrlDecode(Encoding.UTF8.GetString(post_content)); 

或个人价值

 var value1 = WebUtility.UrlDecode(context.Request.Form[param1]);