【问题标题】:POST function in RestSharpRestSharp 中的 POST 函数
【发布时间】:2023-01-08 05:20:14
【问题描述】:

我想通过 RestSharp 将文件发布到 API,但是 Method.Post 遇到错误无法从“RestSharp.Method”转换为“字符串”?Method.POST 的错误是“方法”不包含“POST”的定义

using RestSharp;
using System;
using System.Net;
using System.Net.Http;

namespace UploadToAzure
{
    class Program
    {
        static void Main()
        {
            var client = new RestClient("http://localhost:7071/api/Function1");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddFile("File", "/D:/sample Files/audio0001.mp3");
            IRestResponse response = (IRestResponse)client.Execute(request);
            Console.WriteLine(response.Content);
        }
    }
}

感谢您的回答!

【问题讨论】:

  • RestRequest 可能将一个作为 URL 的字符串作为参数,但您给它一个 Method。查看文档和示例以了解如何使用RestRequest
  • RestSharp 是什么版本?
  • 我怎样才能找到它?

标签: c# restsharp


【解决方案1】:

当我将目标字符串添加到时它解决了休息请求并改变IRestResponse休息反应.另外更正文件的路径。

using RestSharp;
using System;
using System.Net;
using System.Net.Http;

namespace UploadToAzure
{
    class Program
    {
        static void Main()
        {
            var client = new RestClient("http://localhost:7071/api/Function1");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddFile("File", @"D:/sample Files/audio0001.mp3");
            RestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);
        }
    }
}

【讨论】:

    【解决方案2】:

    我有类似的问题,但通过做以下两件事解决了

    1. 将 POST 更改为 Post。
    2. 将 Method.Post 放在双引号 "" 中,即将其视为字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      相关资源
      最近更新 更多