I have API where I need to send request in XML format to get response (in XML too). I think this is called SOAP API but I am not sure so I insert here exectly what I send.
Request should look like this:
<request>
<login>name</login>
<password>password</password>
<hotId>1</hotId>
</request>
And I should send it to this url to get response: https://api.xxx.com/v1/hotel/get
This is how to use this with php and curl:
<?php
$login = '*****';
$password = '*****';
$request =
'<?xml version="1.0"?>' . "\n" .
'<request>' .
'<login>' . htmlspecialchars($login) . '</login>' .
'<password>' . htmlspecialchars($password) . '</password>' .
'<hotId>1</hotId>' .
'</request>';
$ch = curl_init('https://api.xxx.com/x1/hotel/get');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
echo "<pre>\n";
echo htmlspecialchars($response);
echo "</pre>";
So what’s the best way how to do this in C#?
I tried something like this but It´s not working and I think there must be better way.
System.Net.WebRequest req = System.Net.WebRequest.Create(@"https://api.xxx.com/v1/hotel/get");
req.ContentType = "text/xml";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("<?xml version=\"1.0\"?><request><login>login</login><password>pass</password><hotId>1</hotId></request>");
req.ContentLength = bytes.Length;
Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
System.Net.WebResponse resp = req.GetResponse();
if (resp == null) return;
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
string responsecontent = sr.ReadToEnd().Trim();
Edit:
Get method generated with wsdl.exe. I am missing here login and password params to new object[] but don´t know how to add them.
[System.Web.Services.Protocols.SoapRpcMethodAttribute("http://api.xxx.com/v1/hotel/get", RequestNamespace="http://api.xxx.com/v1/hotel/", ResponseNamespace="http://api.xxx.com/v1/hotel/", Use=System.Web.Services.Description.SoapBindingUse.Literal)]
[return: System.Xml.Serialization.XmlElementAttribute("hotel")]
public hotelType get(int hotId) {
object[] results = this.Invoke("get", new object[] {
hotId});
return ((hotelType)(results[0]));
}
Constructor:
public HotelService() {
this.Url = "http://api.xxx.com/v1/hotel/";
}
The mode of communication you describe is a SOAP service.
In .NET, the easiest way to consume one of these services is to have the WSDL (Web Service Definition Language) file. This file contains the metadata regarding the exposed web service: where it is, what methods are available, what data types are produced and accepted by the methods, what transport and/or security layers should be used, etc.
You should be able to get the WSDL from the web service provider; you can either download it or reference it from its hosted location. Once you have this file or know its location, all you have to do is go into VS, right-click on the project containing the code that needs to use the web service, and click “Add Service Reference…”. Then type in the location of the WSDL (this is designed to look for hosted WSDL files, not ones you’ve hosted, but it will work both ways) and you should see the information about the service. Click OK, and VS will auto-generate a proxy class and any custom data types needed, which when called will form and transmit the proper SOAP message and wait for the response.
Then, you just new up an instance of the service and work with it like any class.