首页 > Windows开发 > 详细

c#对接SAP WebService接口

时间:2021-05-26 14:29:10      阅读:141      评论:0      收藏:0      [点我收藏+]

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             }

 

c#对接SAP WebService接口

原文:https://www.cnblogs.com/weifeng123/p/14812866.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!