post方式调用wsdl

首先使用soapui调用获取到入参和出参
在这里插入图片描述
可以看到图片里面的入参和出参

接下来就是拼接xml

string soapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" 
                        + " xmlns:ser=\"http://com.yihu.wsgw/ServiceGateWay\">"
                    + "<soapenv:Header/>"
        + "<soapenv:Body>"
        + "<ser:service>"
        + "<ser:authInfo>{ClientVersion:1,ClientId:100123,'Sign':'Sign',SessionKey:'SessionKey'}</ser:authInfo>"
        + "<ser:sequenceNo>20230410091222123</ser:sequenceNo>"
        + "<ser:api>msg.msgApi.SendMsg</ser:api>"
        + "<ser:param><![CDATA[<Req><Service><Data><Content>"+ content + "</Content></Data>"
        + "<ModeType>20200724</ModeType><Mobile>" + mobile + "</Mobile></Service><TransactionCode>5001</TransactionCode></Req>]]>"
        + "</ser:param>"
        + "<ser:paramType>1</ser:paramType><ser:outType>1</ser:outType><ser:v>1.0</ser:v>"
        + "</ser:service>"
        + "</soapenv:Body>"
        + "</soapenv:Envelope>";
                    
                    var str = Post(soapXml, "http://com.yihu.wsgw/ServiceGateWay/ServiceGateWayPortType/serviceRequest");
                    var ResultValue = jxSoapXml(str);
                    XmlDocument doc = new XmlDocument(); 
                    doc.LoadXml(ResultValue);
                    var J = JObject.Parse(JsonConvert.SerializeXmlNode(doc));
                    if (J.First.First.First.Next.First.ToString() == "10000")
                    {
                        re = "0";
                    }

请注意上面代码块 var str = Post 这里,第二个地址不是post发送的url地址,而是soapAction地址,在soapui请求里面可以查看到这个参数。

        private static string Post(string soapXml, string soapAction)
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[] postData = encoding.GetBytes(soapXml);
            var uri = new Uri(new SystemNodeSetManagerBusiness().GetModel("WX04").ParamValue);
            
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(uri);

            myRequest.Method = "POST";
            myRequest.ContentType = "text/xml;charset=UTF-8";
            myRequest.ContentLength = postData.Length;
            myRequest.PreAuthenticate = true;
            myRequest.AllowWriteStreamBuffering = false;
            myRequest.SendChunked = false;
            myRequest.KeepAlive = true;
            myRequest.Timeout = int.MaxValue;
            myRequest.UserAgent = "Apache-HttpClient/4.1.1 (java 1.5)";
            myRequest.Headers.Add("SOAPAction", soapAction);


            //发送数据
            Stream newStream = myRequest.GetRequestStream();
            // Send the data.
            newStream.Write(postData, 0, postData.Length);
            newStream.Flush();
            newStream.Close();

            //返回响应
            HttpWebResponse myResponse=null;
            try
            {
                myResponse = (HttpWebResponse)myRequest.GetResponse();
                //StreamReader sr = new StreamReader(myRequest.GetResponse().GetResponseStream(), Encoding.UTF8);
                //String retXml = sr.ReadToEnd();
                //sr.Close();
            }
            catch (WebException er)
            {
                myResponse = (HttpWebResponse)er.Response;
                Log4NetUtil.LogInfo("er.Message:" + er.GetBaseException().Message+ "er.StackTrace:" + er.GetBaseException().StackTrace);
                er.ToLog();
            }
            if (myResponse != null && myResponse.StatusCode == HttpStatusCode.OK)
            {
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);

                var str = reader.ReadToEnd();
                return str;
            }
            else
            {
                return string.Empty;
            }

        }

       

上面的代码块是请求发送请求,
new SystemNodeSetManagerBusiness().GetModel(“WX04”).ParamValue 是post的接口地址

 private string jxSoapXml(string strxml)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(strxml);//Load加载XML文件,LoadXML加载XML字符串

            return xmlDoc.DocumentElement["soap:Body"]["ns1:serviceResponse"]["ns1:out"].InnerText;
        }

上面代码块是把请求的xml格式话一下

注意body后面 [“ns1:serviceResponse”] ns1是名称空间,可以参考前面的图片出参。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值