利用HttpWebRequest Post数据 中文编程乱码

yrcy0418 2008-06-13 12:39:30
postrequest.Method = "POST";
//postrequest.Referer = "http://www.baidu.com";
postrequest.Accept = @"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
//postrequest.KeepAlive = true;
postrequest.ContentType = "application/x-www-form-urlencoded";//Post所有
postrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)";
postrequest.CookieContainer = cc;
string pd = "ct=" + ct + "&tn=baiduSubmitThread&word=" + tbname + "&lm=" + lm + "&z=0&sc=0&cm=0&bs=" + bs + "&str2="+ str2 +"&str3=" + str3 + "&str4=" + str4 + "&bu=http://post.baidu.com/f?kw=" + tbname + "&ti=" + subject + "&co=" + context + "&str1=http%3A%2F%2F&ch1=on&Submit3=%B7%A2%B1%ED%CC%F9%D7%D3";
byte[] PostData = System.Text.Encoding.ASCII.GetBytes(pd);
postrequest.ContentLength = PostData.Length;//设定请求字节的长度
Stream smp = postrequest.GetRequestStream();//得到请求流
smp.Write(PostData, 0, PostData.Length);//写入请求流
smp.Close();//关闭流
response = (HttpWebResponse)postrequest.GetResponse();//得到请求流
using (Stream sm = response.GetResponseStream())//得到响应流
{
using (StreamReader sr = new StreamReader(sm, System.Text.Encoding.Default))
{
string strResult = sr.ReadToEnd();//把服务器返回的结果赋值给字符串
response.Close();//关闭响应流
if (strResult.IndexOf("操作成功") > 1)
{
return true;
}
else
{
return false;
}
}
}


这段post数据的程序,其中subject 和 context 两个变量都是中文,post服务器后,页面现实的都是????
...全文
708 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yrcy0418 2008-06-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ericzhangbo1982111 的回复:]
using (StreamReader sr = new StreamReader(sm, System.Text.Encoding.GetEncoding("GB2312")))
[/Quote]

不对,那个是设置读出来的网页编码的

自己找到了,是这个

byte[] PostData = System.Text.Encoding.ASCII.GetBytes(pd);

改成

byte[] PostData = System.Text.Encoding.Default.GetBytes(pd);
ericzhangbo1982111 2008-06-13
  • 打赏
  • 举报
回复
using (StreamReader sr = new StreamReader(sm, System.Text.Encoding.GetEncoding("GB2312")))
lingxyd_0 2008-06-13
  • 打赏
  • 举报
回复
楼上的 正解!

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧