还是关于接口中的编码问题,求高手!

taz01 2010-11-13 11:38:14
昨天我拿到某一公司的API说明文档:他会以get方式请求我公司网站上的指定接口(这个接口我正在做),
URL如下:"http://localhost/pages/ProcessRecord/XXX.aspx?uid=&name=&city=&address等N多参数,
并且中文用gb2312来编码!
好了,现在我要模拟这个get请求,好来做这个接口!

比如A页面是请求页,B页面是接收页!
A页面代码如下:

protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect(GetUrl());
}
protected string GetUrl()
{
StringBuilder sb = new StringBuilder();
sb.Append("http://localhost/pages/ProcessRecord/B.aspx?");
//省略了一大堆参数全是英文版的....
sb.AppendFormat("&name={0}", HttpUtility.UrlEncode("姓名", Encoding.GetEncoding("gb2312")));
sb.AppendFormat("&province={0}", HttpUtility.UrlEncode("广西", Encoding.GetEncoding("gb2312")));
sb.AppendFormat("&city={0}", HttpUtility.UrlEncode("平南", Encoding.GetEncoding("gb2312")));
sb.AppendFormat("&area={0}", HttpUtility.UrlEncode(string.Empty, Encoding.GetEncoding("gb2312")));
sb.AppendFormat("&address={0}", HttpUtility.UrlEncode("广州市天河东", Encoding.GetEncoding("gb2312")));
return sb.ToString(); }

好了,现在在B页面处理了:

protected void Page_Load(object sender, EventArgs e)
{
string u = Request["name"].ToString();
string uu = HttpUtility.UrlDecode(u, System.Text.Encoding.GetEncoding("gb2312"));
结果是u:����
uu:����
}



请问如何来处理请求过来的gb2312?
基本业务都写好代码了,就差这个gb2312编码的问题!
...全文
145 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
huolingfeng 2010-11-15
  • 打赏
  • 举报
回复
那这样吧,根据查询串自己处理
string qs = Request.Url.Query;
string encodeName = getQValue(qs,"name") //自己实现提取querystring中的值
string u = HttpUtility.UrlDecode(encodeName, Encoding.GetEncoding("gb2312"))
边城的刀声 2010-11-15
  • 打赏
  • 举报
回复
把 web.config 的RequestEncoding="gb2312"
protected void Page_Load(object sender, EventArgs e)
{
string u = Request["name"].ToString();
Response.Write(u);
}

taz01 2010-11-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 huolingfeng 的回复:]
把 web.config 的RequestEncoding="gb2312" 是可以的,我试了
[/Quote]
还是不行啊,搞死我了、
汗!
taz01 2010-11-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 qingfeng_wu 的回复:]
HttpUtility.HtmlEncode换这个试试。
[/Quote]
对方指定用gb2312编码的
taz01 2010-11-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zcxverygood123456 的回复:]
在<head></head>之间加这个代码:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-cn" />
[/Quote]
试了,还是一样的!
taz01 2010-11-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 huolingfeng 的回复:]
把u 先按utf-8 做UrlEncode,在按GB2312做UrlDecode 试下
或者该web.config requestEncoding="gb2312" 试试
[/Quote]
问题是接收到的编码都是乱码,正确有编码才可以解码吧!
zcxverygood123456 2010-11-13
  • 打赏
  • 举报
回复
在<head></head>之间加这个代码:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-cn" />
吴青峰 2010-11-13
  • 打赏
  • 举报
回复
HttpUtility.HtmlEncode换这个试试。
huolingfeng 2010-11-13
  • 打赏
  • 举报
回复
把u 先按utf-8 做UrlEncode,在按GB2312做UrlDecode 试下
或者该web.config requestEncoding="gb2312" 试试
huolingfeng 2010-11-13
  • 打赏
  • 举报
回复
把 web.config 的RequestEncoding="gb2312" 是可以的,我试了

62,271

社区成员

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

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

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

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