1、通过软件工具SoapUI获取请求体和响应体
第二步c#调用
1 private static HttpWebRequest CreateWebRequest() 2 { 3 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create 4 (@"http://XXX.XXX.com:8000/sap/bc/srt/rfc/sap/ztest_mes_01/300/ztest_mes_01/ztest_mes_01"); 5 webRequest.Headers.Add(@"SOAP:Action"); 6 webRequest.ContentType = "text/xml;charset=\"utf-8\""; 7 webRequest.Accept = "text/xml"; 8 webRequest.Method = "POST"; 9 string authorization = "<code>UserName</code>" + 10 ":" + "<code>Pw</code>"; 11 byte[] binaryAuthorization = System.Text.Encoding.UTF8.GetBytes(authorization); 12 authorization = Convert.ToBase64String(binaryAuthorization); 13 authorization = "Basic " + authorization; 14 webRequest.Headers.Add("AUTHORIZATION", authorization); 15 return webRequest; 16 }
1 HttpWebRequest request = CreateWebRequest(); 2 XmlDocument soapEnvelopeXml = new XmlDocument(); 3 soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" 4 encoding=""utf-8""?><soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:sap-com:document:sap:soap:functions:mc-style""> 5 <soapenv:Header/> 6 <soapenv:Body> 7 <urn:ZtestMes01/> 8 </soapenv:Body> 9 </soapenv:Envelope> "); 10 11 using (Stream stream = request.GetRequestStream()) 12 { 13 soapEnvelopeXml.Save(stream); 14 } 15 try 16 { 17 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 18 { 19 using (StreamReader rd = new StreamReader(response.GetResponseStream())) 20 { 21 string soapResult = rd.ReadToEnd(); 22 } 23 } 24 } 25 catch (Exception e) 26 { 27 if (e is WebException && ((WebException)e).Status == WebExceptionStatus.ProtocolError) 28 { 29 WebResponse errResp = ((WebException)e).Response; 30 using (Stream respStream = errResp.GetResponseStream()) 31 { 32 using (StreamReader rd = new StreamReader(respStream)) 33 { 34 string soapResult = rd.ReadToEnd(); 35 } 36 } 37 } 38 }
原文:https://www.cnblogs.com/weifeng123/p/14812866.html