首先使用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是名称空间,可以参考前面的图片出参。