c#(POST)动态对接SAP WebService接口
1、通过软件工具SoapUI获取请求体和响应体
第二步c#调用
1 public XmlDocument QueryPostWebService(String URL, String MethodName, string inputPars, string userName, string passWord, string domain) 2 { 3 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL); 4 request.Method = "POST"; 5 request.ContentType = "text/xml; charset=utf-8"; 6 request.Headers.Add("Accept-Language", "zh;");//设置SAP登录语言为中文 7 SetWebRequest(request, userName, passWord, domain); 8 byte[] data = Encoding.UTF8.GetBytes(inputPars); 9 WriteRequestData(request, data); 10 return ReadXmlResponse(request.GetResponse()); 11 } 12 13 private void SetWebRequest(HttpWebRequest request, string userName, string passWord, string domain = "saps4demo.sz-sunway.com") 14 { 15 NetworkCredential cred = new NetworkCredential(userName, passWord); 16 request.Credentials = cred; 17 request.Timeout = 120000; 18 } 19 20 private void WriteRequestData(HttpWebRequest request, byte[] data) 21 { 22 request.ContentLength = data.Length; 23 Stream writer = request.GetRequestStream(); 24 writer.Write(data, 0, data.Length); 25 writer.Close(); 26 } 27 28 private XmlDocument ReadXmlResponse(WebResponse response) 29 { 30 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); 31 String retXml = sr.ReadToEnd(); 32 sr.Close(); 33 XmlDocument doc = new XmlDocument(); 34 doc.LoadXml(retXml); 35 return doc; 36 }
注意:
SOAP V 1.1 --->参数使用:“soapenv:” 命名空间“http://schemas.xmlsoap.org/soap/envelope/” request.ContentType="text/xml; charset=utf-8"
SOAP V 1.2 --->参数使用:“soap:” 命名空间“http://www.w3.org/2003/05/soap-envelope” request.ContentType="application/soap+xml; charset=utf-8"