简体   繁体   中英

issue connecting RestSharp POST request to web API with ASP.NET Core MVC controller

I have a ASP.NET core MVC controller in VS2017 and I'm trying to make a rest request in the following manner:

        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);

and the receiving class is:

[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)
    {
    }
}
}

and the response i get is a timeout exception. What can be the reason for this exception? Am I implementing the rest call in an incorrect way?

Your RestSharp POST request works on my side . The first step is to confirm that remote server post action works as expect , try to use Fiddler or Postman to do the post request and debug the web api to confirm that .

Or you can try to extend the timeout of request :

request.Timeout = 300000;

The problem was that the connection was marked as SSL. Once I removed the checkbox of EnableSSL all worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM