【问题标题】:Getiing web API path from swagger从 swagger 获取 Web API 路径
【发布时间】:2019-08-02 23:39:43
【问题描述】:

我有招摇网址http://somehost/swagger/index.html 结束方法如图所示:

正如有人对我说的那样,http://somehost/api/Referral/GetReferralByNumber 是 API 地址,我可以通过 HTTP 请求引用它。

static void Main(string[] args)
        {

            try
            {
                System.Net.WebClient client = new System.Net.WebClient();
                string result = client.DownloadString("http://somehost/api/Referral/GetReferralByNumber");
            }
            catch (System.Net.WebException ex)
            {

                Console.WriteLine(ex);

            }

            Console.ReadKey();
        }

这是测试 API 的代码,但是

System.Net.WebException:远程服务器返回错误:(404) 未找到异常

有什么帮助吗?

【问题讨论】:

    标签: c# api web-services swagger


    【解决方案1】:

    Client.DownloadString() 发出 GET 请求。您的操作支持 POST。尝试使用HttpClient,它应该更适合您的情况。

    【讨论】:

      【解决方案2】:

      您正在点击 Get 请求,而对于 Get 请求,没有这样的端点。

      您应该尝试将 HTTP 选项 Post 添加到服务器。

      代码

      private static readonly HttpClient client = new HttpClient();
      HttpResponseMessage response = await client.PostAsJsonAsync(
                      "api/referral/GetReferralByNumber", data);
      

      data 是应该发布到服务器的数据。

      【讨论】:

        【解决方案3】:

        您应该创建一个 http 客户端并像这样使用 POST:

        var method = HttpMethod.Post;
        var endPoint = "http://somehost/api/Referral/GetReferralByNumber";
        var request = new HttpRequestMessage(method, endPoint);
        var client = new HttpClient();
        var response = await httpClient.SendAsync(request);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-08-06
          • 2019-10-07
          • 2013-05-07
          • 2022-01-25
          • 2014-10-16
          • 1970-01-01
          • 2018-03-08
          • 1970-01-01
          相关资源
          最近更新 更多