【问题标题】:issue connecting RestSharp POST request to web API with ASP.NET Core MVC controller使用 ASP.NET Core MVC 控制器将 RestSharp POST 请求连接到 Web API 的问题
【发布时间】:2019-06-05 17:25:17
【问题描述】:

我在 VS2017 中有一个 ASP.NET 核心 MVC 控制器,并且 我正在尝试通过以下方式提出休息请求:

        var baseURL = @"http://localhost:44317/";
        var client = new RestClient(baseURL);

        var request = new RestRequest(@"api/values", Method.POST);
        request.AddJsonBody(new Scan
        {
            sid = scanId.ToString(), scanPath = scansParentPath

        });            
        var response = client.Execute(request);

接收类是:

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    // GET api/values
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2" };
    }
    // POST api/values
    [HttpPost]
    public void Post([FromBody] Scan value)
    {
    }
}
}

我得到的响应是超时异常。 此异常的原因可能是什么?我是否以不正确的方式执行 rest 调用?

【问题讨论】:

    标签: c# rest asp.net-core timeout restsharp


    【解决方案1】:

    您的 RestSharp POST 请求对我有效。第一步是确认远程服务器的 post 操作按预期工作,尝试使用 Fiddler 或 Postman 进行 post 请求并调试 web api 以确认。

    或者你可以尝试延长请求的超时时间:

    request.Timeout = 300000;
    

    【讨论】:

      【解决方案2】:

      问题是连接被标记为 SSL。一旦我删除了 EnableSSL 的复选框,一切正常。

      【讨论】:

        猜你喜欢
        • 2017-08-28
        • 2019-11-13
        • 1970-01-01
        • 2020-03-30
        • 2018-11-26
        • 1970-01-01
        • 1970-01-01
        • 2020-10-16
        • 1970-01-01
        相关资源
        最近更新 更多