2017-06-28 19 views
0

このAPIはXMLを入力として受け取り、XMLが変数にあると仮定しましょうXMLcontentと仮定して、Web APIへのPOST要求を作成する方法についてのガイダンスが必要です(http://server:8100/api/SoftwareProductBuild)。ASP.netのWeb APIのリクエストを作成する方法

私はこのPythonを次のようにしていますが、どのようにC#に変換するのですか?

import requests 
with open("PRE_COMMIT_LA.UM.5.8_524f7fef-5b37-11e7-b4ee-f0921c133f10.xml") as f: 
    body = f.read() 
headers = {'Content-Type': 'application/xml'} 
response = requests.post(
    'http://ctdsapi1:8100/api/SoftwareProductBuild', data=body, headers=headers) 

print "Printing DEV Pool Response\n" 
print response 
print "Done...Printing Dev Pool Response\n" 

print response.ok 
print response.content 
+1

ルックあなたはXMLを送信する必要があるため(あなたのケースでyoullの' StringContent'を渡す必要があり、それは 'PostAsync()'するためのメソッドを持って、あなたはそれをエンドポイントと 'HttpContent'のいくつかのタイプを渡します。 – maccettura

+0

@maccettura - あなたが指し示すことができる既存のリンクはどれですか? –

+0

[ドキュメントのみ](https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v = vs118) .aspx) – maccettura

答えて

0

ほとんどの場合、次のようなものがあります。最も適切なのはPostAsXmlAsync methodです。 `HttpClient`で

// In whatever async method 
    // Assuming actual file? Add applicable checks as well. 
    var xml = File.ReadAllText(fullPath + "PRE_COMMIT_LA.UM.5.8_524f7fef-5b37-11e7-b4ee-f0921c133f10.xml"); 

    using (var client = new System.Net.Http.HttpClient()) 
    { 
     var response = await client.PostAsXmlAsync("http://ctdsapi1:8100/api/SoftwareProductBuild", xml); 

     if (!response.IsSuccessStatusCode) 
     { 
      throw new InvalidUriException("Some error with details.")); 
     } 
     Console.WriteLine("Printing DEV Pool Response\n"); 
     ...etc. 
    }