从MS的源代码中分析出来了我的Bug

    近日写一个银行的接口程序,是通过HTTP通讯的。
    我开始选择了System.Net.WebClient的UploadValues来实现,后来发现银行那边用的编码是GB2312,而WebClient似乎是不能指定编码的(.Net1.1和.2.0似乎都是这样),所以通讯是字母和数字的话没有问题,如果通讯是中文的话就出现乱码,导致银行验证数字证书的时候出错。
    走头无路的时候发现.Net2.0还有更低级的HTTP通讯类:WebResponse和WebRequest,所以用这两个类来实现我的目的。结果让我很失望,竟然数字和字母也不能正确发送了。没办法,我自己做了一个调试页面,其实就是把一个空页面设置成<%@ Page ... Trace="true"%>,让我的接口发送数据到这个空页面上,这样可以方便的看到每个Get和Post的字段。
    这个时候看到中文是正常的(也就是说我发送和接受的数据是一致的),英文和数字也都正常,但是“+”传递过程中变成了空格。
    为了这个问题郁闷了很久也没有解决,这时我突然想起来既然WebResponse和WebRequest比WebClient更低级,那么WebResponse会不会是用WebResponse和HttpWebRequest来实现的呢?
    我找来.Net Reflector查看.Net的System.Net.WebClient.UploadValues()源代码如下:
 1 None.gif public   byte [] UploadValues( string  address,  string  method, NameValueCollection data)
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif      byte[] buffer1;
 4InBlock.gif      try
 5ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
 6InBlock.gif            this.m_responseHeaders = null;
 7InBlock.gif            if (this.m_headers == null)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 9InBlock.gif                  this.m_headers = new WebHeaderCollection();
10ExpandedSubBlockEnd.gif            }

11InBlock.gif            string text1 = this.m_headers["Content-Type"];
12InBlock.gif            if ((text1 != null&& (string.Compare(text1, "application/x-www-form-urlencoded"true, CultureInfo.InvariantCulture) != 0))
13ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
14InBlock.gif                  throw new WebException(SR.GetString("net_webclient_ContentType"));
15ExpandedSubBlockEnd.gif            }

16InBlock.gif            this.m_headers["Content-Type"= "application/x-www-form-urlencoded";
17InBlock.gif            WebRequest request1 = WebRequest.Create(this.GetUri(address));
18InBlock.gif            request1.Credentials = this.Credentials;
19InBlock.gif            this.CopyHeadersTo(request1);
20InBlock.gif            request1.Method = method;
21InBlock.gif            using (Stream stream1 = request1.GetRequestStream())
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                  using (StreamWriter writer1 = new StreamWriter(stream1, Encoding.ASCII))
24ExpandedSubBlockStart.gifContractedSubBlock.gif                  dot.gif{
25InBlock.gif                        string text2 = string.Empty;
26InBlock.gif                        foreach (string text3 in data.AllKeys)
27ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
28InBlock.gif                              string text4 = text2 + WebClient.UrlEncode(text3) + "=" + WebClient.UrlEncode(data[text3]);
29InBlock.gif                              writer1.Write(text4);
30InBlock.gif                              text2 = "&";
31ExpandedSubBlockEnd.gif                        }

32InBlock.gif                        writer1.Write("\r\n");
33ExpandedSubBlockEnd.gif                  }

34ExpandedSubBlockEnd.gif            }

35InBlock.gif            WebResponse response1 = request1.GetResponse();
36InBlock.gif            this.m_responseHeaders = response1.Headers;
37InBlock.gif            buffer1 = this.ResponseAsBytes(response1);
38ExpandedSubBlockEnd.gif      }

39InBlock.gif      catch (Exception exception1)
40ExpandedSubBlockStart.gifContractedSubBlock.gif      dot.gif{
41InBlock.gif            if (!(exception1 is WebException) && !(exception1 is SecurityException))
42ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
43InBlock.gif                  throw new WebException(SR.GetString("net_webclient"), exception1);
44ExpandedSubBlockEnd.gif            }

45InBlock.gif            throw;
46ExpandedSubBlockEnd.gif      }

47InBlock.gif      return buffer1;
48ExpandedBlockEnd.gif}
比较我的代码后发现错误在
None.gif string  text4  =  text2  +  WebClient.UrlEncode(text3)  +   " = "   +  WebClient.UrlEncode(data[text3]);
我没有用UrlEncode编码 所以导致加号不能正常传输,所以问题解决。
另外我想我也找到了为什么System.Net.WebClient.UploadValues()不能指定编码
很可能跟这个语句有关系
None.gif StreamWriter writer1  =   new  StreamWriter(stream1, Encoding.ASCII)
微软在代码里边把编码写死了,这个算不算MS的Bug呢?

转载于:https://www.cnblogs.com/wormday/archive/2006/10/24/538509.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值