.Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)

最近在项目中与别的公司对接业务,对方是Java语言,需要调用对方的WebServices,结果常规的添加web引用的方法可以传过去值,但是返回值为null

查了很多资料,没有解决方法

思考应该是.Net封装后,转换成.Net变量时有问题

于是考虑采用基础的HttpWebRequest,使用Soap协议进行WebServices调用

            try
            {
                Console.WriteLine("新开始进行连接测试");

                string responseString;
                string param = @"<?xml version=""1.0"" encoding=""utf-8""?>
                                <soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
                                 <soap12:Body>
                                    <HelloWorld xmlns=""http://tempuri.org/"">
                                        <StudentName>1212</StudentName>
                                         <PassWord>12121</PassWord>
                                    </HelloWorld>
                                </soap12:Body>
                            </soap12:Envelope>";

                byte[] bs = Encoding.UTF8.GetBytes(param);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("  http://fox-gaolijun/Short_Message/Service1.asmx");
                
                myRequest.Method = "POST";
                myRequest.ContentType = "application/soap+xml; charset=utf-8";
                myRequest.ContentLength = bs.Length;
                
                Console.WriteLine("完成准备工作");

                using (Stream reqStream = myRequest.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);
                }
                
                using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
                {
                    StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    responseString = sr.ReadToEnd();
                    Console.WriteLine("反馈结果" + responseString);
                }
                Console.WriteLine("完成调用接口");
            }
            catch (Exception e)
            {
                Console.WriteLine(System.DateTime.Now.ToShortTimeString() + "LBS EXCEPTION:" + e.Message);
                Console.WriteLine(e.StackTrace);
            }

注意多线程的时候可以参考下面的文章进行优化

https://blog.csdn.net/superhoy/article/details/18598835

参考文章:

http://www.cnblogs.com/macroxu-1982/archive/2009/12/23/1630415.html

原文地址:https://www.cnblogs.com/leiyongbo/p/10431733.html