编程语言
首页 > 编程语言> > C# .net core使用HttpClient方式调用WebService

C# .net core使用HttpClient方式调用WebService

作者:互联网

// 声明接口入参(多个参数在此累加)

// objData=>对象格式要转成json string

var parameters= new Dictionary<string, string>

{
{ "para1", objData},
{ "para2", "test" },
};
HttpContent httpContent = new FormUrlEncodedContent(parameters);

// contentType对应 webservice提示 如下图
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.PostAsync("https://localhost:44329/WebService1.asmx/HelloWorld", httpContent).Result;
var statusCode = response.StatusCode.ToString();
var result = response.Content.ReadAsStringAsync().Result;
var doc = new XmlDocument();
doc.LoadXml(result);

// xml返回值数据所在标签,替换成你的xml结果标签,如下图

var status = doc.DocumentElement["Status"].InnerXml;

 

 

标签:core,WebService,C#,doc,var,new,httpContent,response,HttpClient
来源: https://www.cnblogs.com/benneryang/p/15775237.html