C# HttpWebResponse和编码(奇怪字符)
我的问题是,当我将帖子发送到表单时,它们是错误的字符 我发送扩展ASCII码: █████████ 发帖后我得到: –––ˆ–ˆ–ˆ–ˆ–ˆ–ˆ–ˆ–ˆ 我的代码:C# HttpWebResponse和编码(奇怪字符),c#,encoding,httpwebresponse,C#,Encoding,Httpwebresponse,我的问题是,当我将帖子发送到表单时,它们是错误的字符 我发送扩展ASCII码: █████████ 发帖后我得到: –––ˆ–ˆ–ˆ–ˆ–ˆ–ˆ–ˆ–ˆ 我的代码: req = (HttpWebRequest)HttpWebRequest.Create("http://forum.com/); req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"; req.
req = (HttpWebRequest)HttpWebRequest.Create("http://forum.com/);
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
req.Method = "POST";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers.Add("Accept-Language: en-us,en;q=0.5");
req.Headers.Add("Accept-Encoding: gzip,deflate");
req.Headers.Add("Accept-Charset: ISO-8859-1;q=0.7,*;q=0.7");
req.KeepAlive = true;
req.Headers.Add("Keep-Alive: 300");
req.Referer = "http://www.google.com/";
req.ContentType = "application/x-www-form-urlencoded";
req.CookieContainer = _cookieJar;
req.ServicePoint.Expect100Continue = false;
byte[] bytedata =
Encoding.GetEncoding("iso-8859-1").GetBytes("subject=" + HttpUtility.UrlEncode(subject.Replace("_", " ")) +
"&description=" + HttpUtility.UrlEncode(description));
Stream requestStream = req.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();
try
{
response = (HttpWebResponse)req.GetResponse();
}
catch (Exception ex)
{
MessageBox.Show("oh noes...");
}
break;
现场编码为ISO-8859-1。我做到了:
private string Encode(string text)
{
text = HTMLEncodeSpecialChars(text);
return HttpUtility.UrlEncode(text);
}
public string HTMLEncodeSpecialChars(string text)
{
StringBuilder sb = new System.Text.StringBuilder();
foreach (char c in text)
{
if (c > 127) // special chars
sb.Append(String.Format("&#{0};", (int)c));
else
sb.Append(c);
}
return sb.ToString();
}
你说“后发”你得到了奇怪的字符。你的意思是服务器用这种方式解释字符吗?或者你的客户端,当你读取响应时,得到了奇怪的字符。你是否检查了
bytedata
数组以验证你发送的字节是ISO-8859-1?你说你收到的在我看来非常像UTF-8…妈ybe是另一个例子。我有如下字符串:█████████ & 我讨厌扩展ASCII…然后我使用HttpUtility.UrlEncode对文本进行编码。我得到这样的结果:%e2%96%88%e2%96%88%e2%88%e2%96%88%e2%96%88%e2%96%88%e2%96%88%e2%96%88+26+I+hate+extended+ASCII…这是错误的。应该是这样的(我用fiddler和google chrome阅读):%26%239608%3B%26%239608%3B%26%239608%3B%26%239608%3B%26%239608%3B%26%239608%3B%26%239608%3B%26%239608%3B+%26+I+hate+extended+ASCII