• .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

  • 相关阅读:
    PAT乙级1014.福尔摩斯的约会 (20)(20 分)
    PAT乙级1013.数素数
    PAT乙级1012.数字分类 (20)(20 分)
    PAT乙级1011.A+B和C (15)(15 分)
    PAT乙级1025.反转链表 (25)
    PAT乙级1020.月饼(20)
    PAT乙级1015.德才论(25)
    PAT乙级1010.一元多项式求导(25)
    PAT乙级1009.说反话(20)
    PAT乙级1008.数组元素循环右移问题(20)
  • 原文地址:https://www.cnblogs.com/leiyongbo/p/10431733.html
Copyright © 2020-2023  润新知