高手请指点HttpWebRequest乱码问题!急啊~

大川川- 2007-05-09 11:36:43
我在向站点发送请求时,只要有中文就乱码!
string strData = "";

Encoding encoding = Encoding.GetEncoding("GB2312");
byte[] data = encoding.GetBytes( strPostData );

// 准备请求...
try
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myRequest.Method = "POST";
myRequest.Timeout = 60000;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();

// 发送数据
newStream.Write(data, 0, data.Length);
newStream.Close();

//得到网页的原文件
HttpWebResponse response = ( HttpWebResponse )myRequest.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader( resStream, System.Text.Encoding.UTF8 );
strData = sr.ReadToEnd();
resStream.Close();
sr.Close();
response.Close();

}
catch ( Exception ex )
{
Response.Write( ex.Message );
}
return strData ;
...全文
354 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
feisunsunnys 2010-11-10
  • 打赏
  • 举报
回复
myRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
改这,加个charset=utf-8
greatbag 2007-06-05
  • 打赏
  • 举报
回复
StreamReader sr = new StreamReader( resStream, encoding);试试
sanniko 2007-06-05
  • 打赏
  • 举报
回复
先分析好你请求的页面用的什么编码。然后用上面写的收数据的方法看下
sanniko 2007-06-05
  • 打赏
  • 举报
回复
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
namespace SHGridLibrary
{
/// <summary>
/// SendAndReceiveData 的摘要说明。
/// </summary>
public class SendAndReceiveData
{
/// <summary>
/// 发送数据
/// </summary>
/// <param name="Data">数据</param>
/// <param name="URL">URL地址</param>
/// <param name="ReturnData">得到的返回数据</param>
/// <returns></returns>
public bool SendHTTPData(string Data,string URL,out byte[] ReturnData)
{
ReturnData=null;
string _Url=URL;
if(_Url==null||_Url.Trim()=="")
{
Logging.WriteErrorEntry("SendAndReceiveData.SendHTTPData","URL地址为空\r\n"+Data);
return false;
}
else
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(_Url);
try
{
myRequest.Method = "POST";
myRequest.ContentType="text/xml";
myRequest.Timeout=3000;
myRequest.KeepAlive=false;
Encoding enc = System.Text.Encoding.GetEncoding("GB2312");
byte[] _Data=enc.GetBytes(Data);
myRequest.ContentLength = _Data.Length;
Stream newStream=myRequest.GetRequestStream();
newStream.Write(_Data,0,_Data.Length);
newStream.Close();
ReturnData=GetDataFromQU(myRequest);
return true;
}
catch(Exception Ex)
{
if(myRequest!=null)
{
myRequest.Abort();
myRequest=null;
}
Logging.WriteErrorEntry("SendAndReceiveData.SendHTTPData",Ex.ToString()+"\r\n"+_Url+"\r\n"+Data);
return false;
}
}
}
/// <summary>
/// 获得返回的数据
/// </summary>
/// <param name="MyRequest">HttpwebRequest对象</param>
/// <returns></returns>
private byte[] GetDataFromQU(HttpWebRequest MyRequest)
{
string _tmpData=String.Empty;
byte[] _ResultData=null;
HttpWebResponse res=null;
try
{
res = (HttpWebResponse) MyRequest.GetResponse();
Encoding enc=System.Text.Encoding.GetEncoding("GB2312");
//这里判断是不是获得了返回的数据流
StreamReader sr = new StreamReader(res.GetResponseStream(),enc);
_tmpData=sr.ReadToEnd();
sr.Close();
res.Close();
_ResultData=enc.GetBytes(_tmpData);
}
catch(Exception Ex)
{
//这里做一个异常的错误处理,及时释放资源,现在资源占用会导致IIS死掉
//MyRequest.Abort();
//MyRequest=null;
if(res!=null)
{
res.Close();
res=null;
}
Logging.WriteErrorEntry("SendAndReceiveData.GetDataFromQU",Ex.ToString()+"\r\n无法获得对方的HTTP数据返回流!");
}
return _ResultData;
}
}
}
wangfei1204 2007-06-05
  • 打赏
  • 举报
回复
mark
大川川- 2007-05-09
  • 打赏
  • 举报
回复
提交数据<lastname>先生</lastname><firstname>刘</firstname>
返回数据 <firstname>Áõ</firstname><lastname>ÏÈÉú</lastname>
高人吃饭时帮小弟看看阿!
大川川- 2007-05-09
  • 打赏
  • 举报
回复
孟子老大来了!我是直接从页面取来的数据!
大川川- 2007-05-09
  • 打赏
  • 举报
回复
chenguang79(小虫)
我把resStream, System.Text.Encoding.UTF8 改为什么?
谢谢!
孟子E章 2007-05-09
  • 打赏
  • 举报
回复
先看看原来的是什么编码
大川川- 2007-05-09
  • 打赏
  • 举报
回复
strPostData 中有中文就变成Áõ

我已经把UTF8 改为 System.Text.Encoding.Default
还是不行!
急啊,老大们帮忙啊!!!!!!!!!11
chenguang79 2007-05-09
  • 打赏
  • 举报
回复
StreamReader sr = new StreamReader( resStream, System.Text.Encoding.UTF8 );
这里也得把编码改一下

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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