In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "how ASP.NET Core uses HttpClient to call WebService". The content is detailed, the steps are clear, and the details are handled properly. I hope this "ASP.NET Core how to use HttpClient to call WebService" article can help you solve your doubts.
First, create a WebService
We use VS to create a WebService and add a PostTest method with the following code
Summary description of using System.Web.Services;namespace WebServiceDemo {/ WebTest / [WebService (Namespace = "http://tempuri.org/")] [WebServiceBinding (ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem (false)] / / to allow this Web service to be called from a script using ASP.NET AJAX, uncomment to the following line. / / [System.Web.Script.Services.ScriptService] public class WebTest: System.Web.Services.WebService {[WebMethod] public string HelloWorld () {return "HelloWorld";} [WebMethod] public string PostTest (string para) {return $"return parameter {para}";}
After the creation is complete, we release the WebService and deploy it to IIS. Make sure you can browse normally in IIS.
Use HttpClient to call WebService
We use VS to create an ASP.NET Core WebAPI project, and since we are using HttpClient, we first inject it into the ConfigureServices method
Public void ConfigureServices (IServiceCollection services) {/ / inject HttpClient services.AddHttpClient (); services.AddControllers ();}
Then add a controller named WebServiceTest, add a Get method to the controller, and call WebService in the Get method. The controller code is as follows
Using Microsoft.AspNetCore.Mvc;using System;using System.Collections.Generic;using System.Net;using System.Net.Http;using System.Threading.Tasks;using System.Xml;namespace HttpClientDemo.Controllers {[Route ("api/WebServiceTest")] [ApiController] public class WebServiceTestController: ControllerBase {readonly IHttpClientFactory _ httpClientFactory / public WebServiceTestController (IHttpClientFactory httpClientFactory) {_ httpClientFactory = httpClientFactory;} [HttpGet] public async Task Get () {string strResult = "" is injected through the constructor. Try {/ / url address format: WebService address + method name / / WebService address: http://localhost:5010/WebTest.asmx / / method name: PostTest string url = "http://localhost:5010/WebTest.asmx/PostTest"; / / Parameter Dictionary dicParam = new Dictionary (); dicParam.Add ("para", "1"); / / convert the parameter to HttpContent HttpContent content = new FormUrlEncodedContent (dicParam); strResult = await PostHelper (url, content) } catch (Exception ex) {strResult = ex.Message;} return strResult } / Encapsulation calls WebService / URL address / / Parameter / private async Task PostHelper (string url, HttpContent content) {var result = string.Empty using HttpClient Try {using (var client = _ httpClientFactory.CreateClient ()) using (var response = await client.PostAsync (url, content)) {if (response.StatusCode = = HttpStatusCode.OK) {result = await response.Content.ReadAsStringAsync () XmlDocument doc = new XmlDocument (); doc.LoadXml (result); result = doc.InnerText;} catch (Exception ex) {result = ex.Message;} return result }}}
Then start debugging and view the output
When debugging, you can see the returned results. Take a look at the results returned on the page.
This completes the call to WebService. In the production environment, we can write the URL address in the configuration file, and then read the contents of the configuration file in the program, so that we can call WebService dynamically. We modify the above method to configure the URL address in the appsettings.json file
{"Logging": {"LogLevel": {"Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information"}}, "AllowedHosts": "*", / / url address "url": "http://localhost:5010/WebTest.asmx/PostTest"}"
Modify the Get method of controller
Using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Configuration;using System;using System.Collections.Generic;using System.Net;using System.Net.Http;using System.Threading.Tasks;using System.Xml;namespace HttpClientDemo.Controllers {[Route ("api/WebServiceTest")] [ApiController] public class WebServiceTestController: ControllerBase {readonly IHttpClientFactory _ httpClientFactory; readonly IConfiguration _ configuration / public WebServiceTestController (IHttpClientFactory httpClientFactory, IConfiguration configuration) {_ httpClientFactory = httpClientFactory; _ configuration = configuration;} [HttpGet] public async Task Get () {string strResult = "" is injected through the constructor. Try {/ / url address format: WebService address + method name / / WebService address: http://localhost:5010/WebTest.asmx / / method name: PostTest / / read the URL address / / string set in the configuration file Url = "http://localhost:5010/WebTest.asmx/PostTest"; String url = _ configuration ["url"]; / / Parameter Dictionary dicParam = new Dictionary (); dicParam.Add ("para", "1"); / / convert the parameter to HttpContent HttpContent content = new FormUrlEncodedContent (dicParam); strResult = await PostHelper (url, content) } catch (Exception ex) {strResult = ex.Message;} return strResult } / Encapsulation calls WebService / URL address / / Parameter / private async Task PostHelper (string url, HttpContent content) {var result = string.Empty using HttpClient Try {using (var client = _ httpClientFactory.CreateClient ()) using (var response = await client.PostAsync (url, content)) {if (response.StatusCode = = HttpStatusCode.OK) {result = await response.Content.ReadAsStringAsync () XmlDocument doc = new XmlDocument (); doc.LoadXml (result); result = doc.InnerText;} catch (Exception ex) {result = ex.Message;} return result }}}
This allows you to call WebService dynamically.
Read this, the "ASP.NET Core how to use HttpClient to call WebService" article has been introduced, want to master the knowledge of this article still need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.