C# 调用webservice接口报错: xml无效字符:(Unicode:0x0)

直接用VS引用时,提示“ xml无效xml字符:(Unicode:0x0)”错误,当时就是认为是客户的接口有问题,因为是接口里面报错了。使用soapUI工具调用时返回的数据是全的,说明客户接口确实把全部的数据返回过来了,报错的地方不在他们程序中。

在soapUI工具中请求到的这段xml中有0x0字符,导致这段xml数据复制出来时,只能复制到“0x0”前的数据,后半段数据复制不出来!写的程序调用返回时也只给了一半。

 

 

在xml数据中,确实不应该有“0x0”特殊字符,但是有时候和客户的一些环境不同就避免不了出现这种字符,解决方法有两个:

1.让客户接口返回xml数据时过滤一下。(说归说,但是客户是上帝啊!)

2.只能考虑第二种方法了,这时候使用常规的直接引用webservice不行了。那就使用http调用吧,下面是我的代码

  /// <summary>
        /// 
        /// </summary>
        /// <param name="url">地址(webService)</param>
        /// <param name="body">参数</param>
        /// <returns></returns>
        public object HttpPostWebService(string url, string body)
        {
            StringBuilder soap = new StringBuilder();
            soap.Append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ton=\"http://tongji.org\">");
            soap.Append("<soapenv:Header/>");
            soap.Append("<soapenv:Body>");
            soap.Append("<ton:XMLService>");
            soap.Append("<ton:pInput><![CDATA[" + body + "]]></ton:pInput>");
            soap.Append("</ton:XMLService>");
            soap.Append("</soapenv:Body>");
            soap.Append("</soapenv:Envelope>");



            //发起请求
            try
            {
                Uri uri = new Uri(url);
                WebRequest webRequest = WebRequest.Create(uri);
                webRequest.ContentType = "application/soap+xml; charset=utf-8";
                webRequest.Method = "POST";

                using (Stream requestStream = webRequest.GetRequestStream())
                {
                    byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString());
                    requestStream.Write(paramBytes, 0, paramBytes.Length);
                }

                WebResponse webResponse = webRequest.GetResponse();
                using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
                {
                    object reun = RemoveSpecialCharacters(myStreamReader.ReadToEnd());
                    return reun;
                }
            }
            catch (Exception es)
            {
                return es.Message;
            }

            return null;
        }


        /// <summary>
        /// 替换特殊字符
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        private string RemoveSpecialCharacters(string strValue)
        {
            if (string.IsNullOrEmpty(strValue))
            {
                return string.Empty;
            }
            else
            {
                string strReturnVlaue = strValue;
                List<int> ascIIList = new List<int>();
                //出现ASCII码0~31的采集会报错而其中9,10,13ASCII码对应的字符不会报错
                ascIIList.Add(9);
                ascIIList.Add(10);
                ascIIList.Add(13);

                for (int i = 0; i <= 31; i++)
                {
                    if (ascIIList.Contains(i)) { continue; }
                    strReturnVlaue = strReturnVlaue.Replace(((char)i).ToString(), "");
                }
                return strReturnVlaue;
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值