简体   繁体   中英

ASP.NET Core 5 httpclient using xml

What is the preferred way to use httpClient with XML to make a post & get call?

public class ShippingService : IShippingService
{
    private readonly HttpClient _httpClient;
    private readonly string _remoteServiceBaseUrl;

    public CatalogService(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public async Task<List<ShippingPrice>> GetShippingPrices()
    {
         // httpClient post using xml format
    }
}

Check it:

   HttpClient _httpClient = new HttpClient();
   string xml = ""; //serialize your object
   var content = new StringContent(xml, Encoding.UTF8, "application/xml");
   await _httpClient.PostAsync("http://example.com/api",content);

for serialize object to xml see this: XML Serialize generic list of serializable objects

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