応答を取得するためにXML形式でリクエストを送信する必要があるAPIがあります(XMLでも)。これはSOAPAPIと呼ばれていると思いますが、よくわからないので、送信したものを正確にここに挿入します。
リクエストは次のようになります。
<request>
<login>name</login>
<password>password</password>
<hotId>1</hotId>
</request>
そして、私はそれをこのURLに送信して、応答を得る必要があります。https://api.xxx.com/v1/hotel/get
これは、phpと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>";
では、C#でこれを行うための最良の方法は何ですか?
私はこのようなことを試みましたが、それは機能しておらず、もっと良い方法があるに違いないと思います。
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();
編集:wsdl.exeで生成されたメソッドを取得します。ここに新しいobject[]へのログインとパスワードのパラメータがありませんが、それらを追加する方法がわかりません。
[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]));
}
コンストラクタ:
public HotelService() {
this.Url = "http://api.xxx.com/v1/hotel/";
}