WeatherForecastController.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Microsoft.AspNetCore.Authorization;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System.ServiceModel;
  4. using System.Xml;
  5. namespace DopInterfacePlatform.Controllers
  6. {
  7. [ApiController]
  8. [Route("[controller]/[action]")]
  9. [Authorize]
  10. public class WeatherForecastController : ControllerBase
  11. {
  12. private static readonly string[] Summaries = new[]
  13. {
  14. "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
  15. };
  16. private readonly ILogger<WeatherForecastController> _logger;
  17. public WeatherForecastController(ILogger<WeatherForecastController> logger)
  18. {
  19. _logger = logger;
  20. }
  21. [HttpGet(Name = "GetWeatherForecast")]
  22. [Authorize]
  23. public IEnumerable<WeatherForecast> Get()
  24. {
  25. return Enumerable.Range(1, 5).Select(index => new WeatherForecast
  26. {
  27. Date = DateTime.Now.AddDays(index),
  28. TemperatureC = Random.Shared.Next(-20, 55),
  29. Summary = Summaries[Random.Shared.Next(Summaries.Length)]
  30. })
  31. .ToArray();
  32. }
  33. [HttpGet(Name = "GetByCount")]
  34. [Authorize]
  35. public int GetByCount(int count)
  36. {
  37. return count+7;
  38. }
  39. [HttpGet(Name = "GetByCount1")]
  40. [Authorize]
  41. public int GetByCount1(int count,int count2)
  42. {
  43. return count + 7+count2;
  44. }
  45. [HttpGet(Name = "TestWebservice")]
  46. [Authorize]
  47. public string TestWebservice()
  48. {
  49. var parameters = new Dictionary<string, string> { { "byProvinceName", "湖北" } };
  50. HttpContent httpContent = new FormUrlEncodedContent(parameters);
  51. // contentType对应 webservice提示 如下图
  52. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
  53. HttpClient httpClient = new HttpClient();
  54. HttpResponseMessage response = httpClient.PostAsync("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity", httpContent).Result;
  55. var statusCode = response.StatusCode.ToString();
  56. var result = response.Content.ReadAsStringAsync().Result;
  57. var doc = new XmlDocument();
  58. doc.LoadXml(result);
  59. // xml返回值数据所在标签,替换成你的xml结果标签,如下图
  60. var status = doc.InnerXml;
  61. return status;
  62. }
  63. }
  64. }