将HttpClient与SOAP一起使用

编程入门 行业动态 更新时间:2024-10-28 15:22:26
本文介绍了将HttpClient与SOAP一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在尝试使用.Net Framework 4.7中的HTTPClient对象来创建一个简单的SOAP请求.我已经在Postman中使用了参数,并且效果很好.这是我的代码:

I have been trying to use the HTTPClient object in .Net Framework 4.7 to create a simple SOAP request. I have used the parameters in Postman and it works perfectly fine. Here's my code:

string url = "webservices.oorsprong/websamples.countryinfo/CountryInfoService.wso"; //SOAP envelope string string xmlSOAP = @"<? xml version = ""1.0"" encoding = ""utf-8"" ?> < soap : Envelope xmlns: soap = ""schemas.xmlsoap/soap/envelope/"" > < soap:Body > < ListOfContinentsByName xmlns = ""www.oorsprong/websamples.countryinfo""> </ ListOfContinentsByName > </ soap:Body > </ soap:Envelope >"; HttpClient httpClient = new HttpClient(); HttpContent httpContent = new StringContent(xmlSOAP); HttpResponseMessage response; HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url); req.Headers.Add("SOAPAction", ""); req.Method = HttpMethod.Post; req.Content = httpContent; req.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml; charset=utf-8"); // Here you will get the Reponse from service response = await httpClient.SendAsync(req); // Converting the response into text format var responseBodyAsText = await response.Content.ReadAsStringAsync();

有人知道为什么这在邮递员中有效但不使用HTTPClient吗?我从这里的Microsoft示例( social.msdn.microsoft/Forums/windowsapps/zh-CN/15e0c692-c47c-443c-b96d-5c4f77e7ed66/如何在Windows Phone-8中使用C语言发送接收肥皂请求和响应的方法?forum = wpdevelop ).

Any idea why this works in postman but not using the HTTPClient? I referenced this from the microsoft example here (social.msdn.microsoft/Forums/windowsapps/en-US/15e0c692-c47c-443c-b96d-5c4f77e7ed66/how-to-sendreceive-soap-request-and-response-using-c-in-windows-phone-8?forum=wpdevelop).

推荐答案

您的代码看起来不错并且应该可以工作.看来API服务器现在已关闭.

Your code looks fine and should work. Looks like the API server is down now.

这是相同的代码,但几乎没有修复和改进,只是使其变得稳定

Here's the same code with few fixes and improvements that just makes it stable

public class Program { private static readonly HttpClient httpClient = new HttpClient(); static async Task Main(string[] args) { string url = "webservices.oorsprong/websamples.countryinfo/CountryInfoService.wso"; string xmlSOAP = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:soap=""schemas.xmlsoap/soap/envelope/""> <soap:Body> <ListOfContinentsByName xmlns=""www.oorsprong/websamples.countryinfo""/> </soap:Body> </soap:Envelope>"; try { string result = await PostSOAPRequestAsync(url, xmlSOAP); Console.WriteLine(result); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } private static async Task<string> PostSOAPRequestAsync(string url, string text) { using (HttpContent content = new StringContent(text, Encoding.UTF8, "text/xml")) using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)) { request.Headers.Add("SOAPAction", ""); request.Content = content; using (HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)) { //response.EnsureSuccessStatusCode(); // throws an Exception if 404, 500, etc. return await response.Content.ReadAsStringAsync(); } } } }

可能您还可以另外填写 SOAPAction ,例如使用 url ,但取决于服务器要求.

Probably you may additionally fill SOAPAction e.g. with url, but it depends on server requirements.

更多推荐

将HttpClient与SOAP一起使用

本文发布于:2023-11-14 13:15:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:HttpClient   SOAP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!