Remark : Soap xml 을 조회하는 웹서비스를 만들어 보자
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Xml; namespace SoapTest { /// <summary> /// Service1의 요약 설명입니다. /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // ASP.NET AJAX를 사용하여 스크립트에서 이 웹 서비스를 호출하려면 다음 줄의 주석 처리를 제거합니다. // [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService { //****************************************************************************** // 함수명 : Function // 작성자 : *** // 작성일 : 2022-02-11 // 설명 : Soap 조회간단 테스트 // 리턴값 : // 매개변수 : // Remark : // 이력사항 : //****************************************************************************** [WebMethod] public XmlElement uAPITest(string ServiceName, string SoapRequest) { System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls12; string EndPoint = "https://*****.**./Soap/" + ServiceName; string credentials = "**************"; string Password = "******/"; string Res = string.Empty; try { SoapClass SC = new SoapClass(); Res = SC.CallWebService(1, EndPoint, SoapRequest, credentials, Password); SC = null; } catch(Exception ex) { Res = ex.Message.ToString(); Res = "<ERR>" + Res + "</ERR>"; } XmlDocument doc = new XmlDocument(); doc.LoadXml(Res); return doc.DocumentElement; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Xml; using System.Net; using System.IO; namespace SoapTest { public class SoapClass { public string CallWebService(int ServerType, string EndPoint, string SoapData, string credentials, string Password) { var _action = EndPoint; XmlDocument soapEnvelopeXml = CreateSoapEnvelope(SoapData); HttpWebRequest webRequest = CreateWebRequest(ServerType, _action, credentials, Password); InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest); // begin async call to web request. IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null); // suspend this thread until call is complete. You might want to // do something usefull here like update your UI. asyncResult.AsyncWaitHandle.WaitOne(); //주석처리 // get the response from the completed web request. string soapResult; using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult)) { using (StreamReader rd = new StreamReader(webResponse.GetResponseStream())) { soapResult = rd.ReadToEnd(); } } return soapResult; } private HttpWebRequest CreateWebRequest(int ServerType, string action, string credentials, string Password) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(action); //action 사용 //webRequest.Headers.Add("SOAPAction", action); //아래아 같이 수정 webRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials + ":" + Password))); webRequest.ContentType = "text/xml;charset=\"utf-8\""; webRequest.Accept = "text/xml"; webRequest.Method = "POST"; return webRequest; } private XmlDocument CreateSoapEnvelope(string SoapData) { XmlDocument soapEnvelop = new XmlDocument(); soapEnvelop.LoadXml(SoapData); return soapEnvelop; } private void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) { using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } } } } |