我想知道如何通过POST方法发送ASP.NET中的UTF-8编码字符。
使用我的代码,它可以正常工作但不能使用重音字符(例如ô,ê,éè)。我收到错误500(内部服务器错误)
在php中,我使用函数utf8_encode()和urlencode()之后它正在工作!
谢谢你的帮助
这是我的代码:
WebRequest request = WebRequest.Create("http://api.website.com");
request.Method = "POST";
string postData = "login=xxx&password=xxx&postblog=My message. Problem with accented characters like ô ê é è ";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
...
response.Close();
答案 0 :(得分:1)
不确定是不是这样,但您的ContentType不太正确。试试这个
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";