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 73 74 75 76 77 78 79 80 |
using System.Xml; using System.Net; using System.IO; using System.Text; namespace UniRecord { public class SoapClass { public static string CallWebService() { var _url = "https://test.com/B2BGateway/connect/uAPI"; var _action = "https://test.com/uAPI/UniversalRecordService"; //EndPoint XmlDocument soapEnvelopeXml = CreateSoapEnvelope(); HttpWebRequest webRequest = CreateWebRequest(_url, _action); 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 static HttpWebRequest CreateWebRequest(string url, string action) { string credentials = "ID:Password"; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(action); //action 사용 webRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials))); webRequest.ContentType = "text/xml;charset=\"utf-8\""; webRequest.Accept = "text/xml"; webRequest.Method = "POST"; return webRequest; } private static XmlDocument CreateSoapEnvelope() { XmlDocument soapEnvelop = new XmlDocument(); soapEnvelop.LoadXml(@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:univ='http://www.travelport.com/schema/universal_v31_0' xmlns:com='http://www.travelport.com/schema/common_v31_0'>" + "<soapenv:Header>" + "<univ:SupportedVersions/>" + "</soapenv:Header>" + "<soapenv:Body>" + "<univ:UniversalRecordRetrieveReq TraceId='trace' AuthorizedBy='user' TargetBranch='ID' RetrieveProviderReservationDetails='false' ViewOnlyInd='false'>" + "<com:BillingPointOfSaleInfo OriginApplication='UAPI'/>" + "<univ:UniversalRecordLocatorCode>PNR001</univ:UniversalRecordLocatorCode>" + "</univ:UniversalRecordRetrieveReq>" + "</soapenv:Body>" + "</soapenv:Envelope>"); return soapEnvelop; } private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest) { using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } } } } 삭제 |
참조 : http://stackoverflow.com/questions/4791794/client-to-send-soap-request-and-received-respons