我正在使用网络客户端在此处的后请求中上传值:
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的方法。我对吗?因为显然,它没有按预期工作。
最佳答案
您不能使用非 Ascii 编码的 UploadValues 方法,构建请求的内部方法:
// 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。
关于c# - WebClient.UploadValues 的 UTF32?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8561178/