首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

远路请求API xml 属性中有“+” 对方收到是空格

2013-03-06 
远程请求API xml 属性中有“+” 对方收到是空格远程请求API xml 属性中有“+” 对方收到是空格如abc typeab

远程请求API xml 属性中有“+” 对方收到是空格
远程请求API xml 属性中有“+” 对方收到是空格

<abc type="abc+def">
 abc
</abc>

但是对方收到的是
<abc type="abc def">
 abc
</abc>

“+”变成了空格
一下是我的Http请求代码


  public static string PostXml(string url, string Method, string xml)
       {
           string ResponseResult = "";
           string para = "xml=" + xml;
           HttpWebRequest request = null;
           if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
           {
               ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
               request = WebRequest.Create(url) as HttpWebRequest;
               //request.ProtocolVersion = HttpVersion.Version10;
              
           }
           else
           {
               request = WebRequest.Create(url) as HttpWebRequest;//创建一个请求
               
           }
           request.KeepAlive = false;
           request.Method = Method;
           WebHeaderCollection webHead = new WebHeaderCollection();
           webHead.Add("Accept-Encoding:gzip,deflate");
           request.Headers = webHead;
           request.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
           request.ContentType = "application/x-www-form-urlencoded";
         
           Stream st = null;
           if (Method == "POST")
           {
              
               byte[] data = Encoding.UTF8.GetBytes(para);
               request.ContentLength = data.Length;
               st = request.GetRequestStream();


               st.Write(data, 0, data.Length);
           }
           using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)//请求回发
           {
               string Char = response.CharacterSet;
               string Charr = response.ContentEncoding;
               string Charrr = response.ContentType;
               // Get the response stream
               GZipStream stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
               StreamReader reader = new StreamReader(stream, Encoding.UTF8);
               // Write response to the console
               ResponseResult = reader.ReadToEnd();
               reader.Close();
               response.Close();
           }
           if (st != null)
           {
               st.Close();
               st.Dispose();
               st = null;
           }
           request.Abort();
           request = null;
           return ResponseResult;
       }

       private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
       {
           return true; //总是接受  
       }  


是不是下面代码有问题

byte[] data = Encoding.UTF8.GetBytes(para);
               request.ContentLength = data.Length;
               st = request.GetRequestStream();
               st.Write(data, 0, data.Length);
xml api


[解决办法]
用System.Web.HttpUtility.UrlEncode
和System.Web.HttpUtility.UrlDecode
分别加解码试试

热点排行