• C#调用JS的WebService的方法返回null


    连上了别人的VPN后,使用WebService测试软件测试了一下,结果正常,但是当我在vs里面添加WebService服务,调用的时候就出现了问题,问题如下图:

    后来问了一下服务端那边的同事,他们说服务是用JS写的,使用JS和JAVA调用没有问题。

    然后我用JS调用试了一下,一点问题都没有,返回的结果和测试软件是一样的,都是xml字符串。

    经过很多次百度之后,通过JS调用webservice大概知道了C#调用webservice返回null的原因:因为C#调用webservice的方法返回了一个自定义的对象,在C#自己解析xml字符串的时候没有成功,所以返回null。

    自己的解决办法是:借鉴JS调用webservice方法,在C#通过发送HttpPost请求来调用WebService,这个时候返回的就是xml数据了,需要自己解析。代码如下:

            private void MPointHisData(string strid, string startDt, string endDt, string token,int id)
            {
                string ServerPage = "http://XXX/XXX/XXX?wsdl";
                try
                {
                    string strXml = "<?xml version='1.0' encoding='utf-8'?>";
                    strXml = strXml + "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:impl='http://test.com' xmlns:intf='http://test.com'>";
                    strXml = strXml + "<soap:Body>";
                    strXml = strXml + "<MPointHisData xmlns='http://test.com'>";
                    strXml = strXml + "<id>" + strid + "</id>";
                    strXml = strXml + "<startDt>" + startDt + "</startDt>";
                    strXml = strXml + "<endDt>" + endDt + "</endDt>";
                    strXml = strXml + "<token>" + token + "</token>";
                    strXml = strXml + "</MPointHisData>";
                    strXml = strXml + "</soap:Body>";
                    strXml = strXml + "</soap:Envelope>";
                    string res = HttpConnectToServer(ServerPage, strXml);//返回xml形式的数据
                    if (res == null || res == "")
                    {
                        MessageBox.Show("获取数据失败!");
                        return;
                    }
                    //解析获取的xml字符串
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(res);
                    XmlNode nodebody = doc.GetElementsByTagName("impl:success")[0];
                    if (nodebody.InnerText == "true")//如果获取到数据
                    {
                        foreach (XmlNode resultnode in doc.GetElementsByTagName("impl:result"))
                        {
                            DateTime dataDt = DateTime.Parse(resultnode.ChildNodes[0].InnerText);
                            double value = double.Parse(resultnode.ChildNodes[1].InnerText);
                            di.WriteHistoryData(id, value,dataDt);
                        }
                    }
                    else
                    {
    
                    }
                }
                catch (Exception ex)
                {
    
    
                }
            }
            //发送消息到服务器
            private string HttpConnectToServer(string ServerPage, string strXml)
            {
                byte[] dataArray = Encoding.Default.GetBytes(strXml);
                //创建请求
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage);
                request.Method = "POST";
                request.ContentLength = dataArray.Length;
                request.ContentType = "application/x-www-form-urlencoded";
                //创建输入流
                Stream dataStream = null;
                try
                {
                    dataStream = request.GetRequestStream();
                }
                catch (Exception)
                {
                    return null;//连接服务器失败
                }
                //发送请求
                dataStream.Write(dataArray, 0, dataArray.Length);
                dataStream.Close();
                //读取返回消息
                string res = string.Empty;
                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    res = reader.ReadToEnd();
                    reader.Close();
                }
                catch (Exception ex)
                {
                    return null;//连接服务器失败
                }
                return res;
            }

    昨天一天都在纠结调用WebService的事,终于在下班的时候解决了,还好有同事帮帮忙,今天借着程序处理数据的间隙写写昨天处理问题的经过。

    当服务发送错误的消息时,客户端获取错误消息如下:

    //读取返回消息
                string res = string.Empty;
                HttpWebResponse response;
                try
                {
                   response = (HttpWebResponse)request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    res = reader.ReadToEnd();
                    reader.Close();
                }
                catch (WebException ex)
                {
                    response = (HttpWebResponse)ex.Response;

    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    res = sr.ReadToEnd();
                } 

     当服务出现“服务器无法为请求提供服务,因为不支持该媒体类型”时,修改方法如下:

     byte[] dataArray = Encoding.Default.GetBytes(strXml);
                //创建请求
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage);
                request.Method = "POST";
                request.ContentLength = dataArray.Length;
                request.ContentType = "text/xml;charset=utf-8 "; //修改的地方
                //创建输入流
                Stream dataStream = null;
                try
                {
                    dataStream = request.GetRequestStream();
                }
                catch (Exception)
                {
                    return null;//连接服务器失败
                }
                //发送请求
                dataStream.Write(dataArray, 0, dataArray.Length);
                dataStream.Close();
                //读取返回消息
                string res = string.Empty;
                try
                {
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    res = reader.ReadToEnd();
                    reader.Close();
                }
                catch (WebException ex)
                {
                    HttpWebResponse response = (HttpWebResponse)ex.Response;
    
                    StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                    res = sr.ReadToEnd();
                }
                return res;
  • 相关阅读:
    操作数据库系统(OLTP)和联机分析处理系统(OLAP)的区别
    BI笔记-SSAS部署的几种方式及部署后的SSAS刷新
    概念-数据仓库与元数据
    零基础学Python 3之环境准备
    OFBiz进阶之HelloWorld(五)创建新实体
    OFBIZ bug_create-component ERROR
    OFBIZ bug_ControlServlet.java:233:ERROR
    OFBiz进阶之HelloWorld(三)CRUD操作
    OFBiz进阶之HelloWorld(二)创建热部署模块
    OFBIZ bug_ControlServlet.java:239:ERROR
  • 原文地址:https://www.cnblogs.com/GIScore/p/4875312.html
Copyright © 2020-2023  润新知