简体   繁体   中英

How to create a request for a ASP.net web api

I need guidance on how I can create a POST request to a web API exposed @ http://server:8100/api/SoftwareProductBuild ' this API takes an XML as input, let's assume the XML is in variable XMLcontent

I have done this python as follows, how to convert to 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

Something like the following should get you most of the way there. Most applicable is the PostAsXmlAsync method .

    // 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.
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM