高分求解,关于HttpWebRequest编码传输问题

skpay 2006-05-18 04:53:19
我有一段程序

string lcUrl="http://xxx/test.php?name=中文"
HttpWebRequest loHttp=(HttpWebRequest)WebRequest.Create(lcUrl);
loHttp.Timeout= 120000;// 60 secs
loHttp.UserAgent="Http Request by EL5939";
HttpWebResponse loWebResponse = (HttpWebResponse)loHttp.GetResponse();

调用后,对方获取到的name值是乱码,但是通过IE直接打开http://xxx/test.php?name=中文,看到页面编码是GB2312,而且对方也能正确接受到中文,请问这是为什么呢?

我尝试HttpWebRequest的TransferEncoding但是不会用,不知道是不是这个可以解决乱码问题?


在线盼解,谢谢!
...全文
787 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
levin9 2006-09-23
  • 打赏
  • 举报
回复
up
afhel 2006-09-15
  • 打赏
  • 举报
回复
孟子怎么会有个钻石呢?哪里买的.
wshuangminlg 2006-09-11
  • 打赏
  • 举报
回复
帮LZ顶
huguosheng 2006-09-08
  • 打赏
  • 举报
回复
string url="http://weather.tq121.com.cn/detail.php?city="+city.ToString();
HttpWebRequest request=(HttpWebRequest)WebRequest.Create(url);

HttpWebResponse response=(HttpWebResponse)request.GetResponse();
Stream s=response.GetResponseStream();
StreamReader objreader=new StreamReader (s,Encoding.Default);
jxhguojun 2006-09-08
  • 打赏
  • 举报
回复
Framework提供的只有UTF-8的(估计是这样)
而且net_lover(孟子E章)
的方法也不行
string lcUrl="http://xxx/test.php?name="+ System.Web.HttpUtility.UrlEncode("中文",System.Text.Encoding.GetEncoding("GB2312"));
只在第一次请求的时候有效
如果有跳转
AutoRedirect以后Location就被自动用UTF-8转了一次,但服务器用的还是GB2312,得到的是乱码!!!而不是你想要的GB2312
如果设置AllowAutoRedirect = false;
那么Header["Location"]可能是乱码,转都转不回来
ilovejolly 2006-05-26
  • 打赏
  • 举报
回复
mark
skpay 2006-05-24
  • 打赏
  • 举报
回复
内置的那个无效的,用了还是乱码
孟子E章 2006-05-24
  • 打赏
  • 举报
回复
.NET 已经内置了,不用自己写了
skpay 2006-05-24
  • 打赏
  • 举报
回复
解决如下(不知道哪个人高人重写的编码函数,搜索到的):

protected string UrlEncode(string url)
{
byte[] bs=Encoding.GetEncoding("GB2312").GetBytes(url);
StringBuilder sb=new StringBuilder();
for(int i=0;i<bs.Length;i++)
{
if(bs[i]<128)
sb.Append((char)bs[i]);
else
{
sb.Append("%"+bs[i++].ToString("x").PadLeft(2,'0'));
sb.Append("%"+bs[i].ToString("x").PadLeft(2,'0'));
}
}
return sb.ToString();
}

在调用request之前把URL编码一下就ok了
孟子E章 2006-05-19
  • 打赏
  • 举报
回复
根据不同的编码设置,可以试试

string lcUrl="http://xxx/test.php?name="+ System.Web.HttpUtility.UrlEncode("中文",System.Text.Encoding.GetEncoding("GB2312"));

or

string lcUrl="http://xxx/test.php?name="+ System.Web.HttpUtility.UrlEncode("中文",System.Text.Encoding.UTF8);


or

string lcUrl="http://xxx/test.php?name="+ System.Web.HttpUtility.UrlEncode("中文",System.Text.Encoding.Unicode);等
孟子E章 2006-05-19
  • 打赏
  • 举报
回复
string lcUrl="http://xxx/test.php?name="+Server.UrlEncode("中文");
goldanycom 2006-05-19
  • 打赏
  • 举报
回复
技术交流群号:23266021
欢迎大家在此讨论关于.net的各种技术。1号群
skpay 2006-05-18
  • 打赏
  • 举报
回复
用UrlEncode编码URL再调用,会报这个异常
InvalidURI:The format of the URI could not be determined
skpay 2006-05-18
  • 打赏
  • 举报
回复
URLEncode对方不识别
Anders_lt 2006-05-18
  • 打赏
  • 举报
回复
Server.UrlEncode(url)
shalen520 2006-05-18
  • 打赏
  • 举报
回复
string lcUrl=UrlEncode("http://xxx/test.php?name=中文");

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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