...
HttpClient
Pro větší kontrolu nad kódempřehledný kód, případně pro situace kdy nepoužíváte IDE které umožňuje automaticky generovat potřebný kód na základě WSDL definice, je možné využít třídy HttpClient, která je dostupná ve verzích:
Code Block | ||
---|---|---|
| ||
// Prepare HttpClient var client = new HttpClient(); // Define a new HTTP POST Request var request = new HttpRequestMessage(HttpMethod.Post, "https://creditwebservices.creditcheck.cz/CreditCheckAktualizace.asmx"); // Add SOAPAction header to the Request request.Headers.Add("SOAPAction", "https://creditwebservices.creditcheck.cz/GetSubjectFullReport"); // Add content to the Request var content = new StringContent("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Header>\n <CreditCheckHeader xmlns=\"https://creditwebservices.creditcheck.cz/\">\n <Login>{login}</Login>\n <Password>{password}</Password>\n </CreditCheckHeader>\n </soap:Header>\n <soap:Body>\n <GetSubjectFullReport xmlns=\"https://creditwebservices.creditcheck.cz/\">\n <subjectData xmlns=\"https://creditwebservices.creditcheck.cz/\">\n <requestSubject>\n <subjectType>Company</subjectType>\n <ic>00014915<<ic>{ico}</ic>\n </requestSubject>\n </subjectData>\n </GetSubjectFullReport>\n </soap:Body>\n</soap:Envelope>", null, "text/xml; charset=utf-8"); request.Content = content; // Call Web service var response = await client.SendAsync(request); response.EnsureSuccessStatusCode(); Console.WriteLine(await response.Content.ReadAsStringAsync()); |
HttpWebRequest
Pro kompletní kontrolu nad odesílaným požadavkem, je možné využít třídy HttpWebRequest. Tento postup umožňuje definovat veškeré parametry odchozího Requestu. Bude funkční prakticky ve všech verzích .NET.
Code Block | ||
---|---|---|
| ||
using System.Net;
// Set the SOAP endpoint URL
string url = "https://creditwebservices.creditcheck.cz/CreditCheckAktualizace.asmx";
// Construct the SOAP request body
string soapXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Header>
<CreditCheckHeader xmlns=""https://creditwebservices.creditcheck.cz/"">
<Login>{login}</Login>
<Password>{password}</Password>
</CreditCheckHeader>
</soap:Header>
<soap:Body>
<GetSubjectFullReport xmlns=""https://creditwebservices.creditcheck.cz/"">
<subjectData xmlns=""https://creditwebservices.creditcheck.cz/"">
<requestSubject>
<subjectType>Company</subjectType>
<ic>{ico}</ic>
</requestSubject>
</subjectData>
</GetSubjectFullReport>
</soap:Body>
</soap:Envelope>";
// Create the HTTP request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
// Write the SOAP request to the request stream
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(soapXml);
}
// Get the response from the server
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string responseXml = reader.ReadToEnd();
// Process the SOAP response XML
Console.WriteLine("SOAP Response:");
Console.WriteLine(responseXml);
}
}
else
{
Console.WriteLine("Error: " + response.StatusDescription);
}
} |
Service reference
Využijeme možností které nám nabízí IDE a necháme automaticky vygenerovat potřebné třídy a metody pro volání na základě WSDL definice ke které poskytnete URL (viz. výše).
...
Code Block | ||
---|---|---|
| ||
using System.Xml; // Prepare instance of SOAP client class (class is generated automatically based on WSDL) Creditcheck_ServiceReference.CreditCheckAktualizaceSoapClient client = new Creditcheck_ServiceReference.CreditCheckAktualizaceSoapClient(Creditcheck_ServiceReference.CreditCheckAktualizaceSoapClient.EndpointConfiguration.CreditCheckAktualizaceSoap); // Prepare instance of CreditCheckHeader Creditcheck_ServiceReference.CreditCheckHeader ccHeader = new Creditcheck_ServiceReference.CreditCheckHeader(); ccHeader.Login = "{login}"; ccHeader.Password = "{password}"; // Call WS method GetCreditCheck var getCreditChecksResult = await client.GetCreditChecksAsync(ccHeader); // Define request parameters string gsfrXmlRequest = @" <requestSubject> <subjectType>Company</subjectType> <ic>{ico}</ic> </requestSubject>"; XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(gsfrXmlRequest); var// getCreditChecksResultCall =WS await client.GetCreditChecksAsync(ccHeader);method GetSubjectFullReport var getSubjectFullReportResult = await client.GetSubjectFullReportAsync(ccHeader, xmlDocument); |
...