繁体   English   中英

如何为ASP.net Web API创建请求

[英]How to create a request for a ASP.net web api

我需要有关如何创建对暴露于@ http:// server:8100 / api / SoftwareProductBuild的Web API的POST请求的指导,该API将XML作为输入,假设XML位于变量XMLcontent

我已经按照以下方法完成了此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

类似于以下内容的内容应该可以助您一臂之力。 最适用的是PostAsXmlAsync方法

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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