简体   繁体   中英

Request using RestSharp

I am trying to send a simple request using RestSharp to read the text from a webpage using Get Method in Visual Studio 2017 Community ASP.Net MVC (C#) Web API. But I am getting errors in writing the request. I am only familiar with C# websites and not much familiar with MVC or Core. I am trying the following Code:

public void Get(){
   var client = new RestClient("http://www.dictionary.com/browse/computer");
   var request = new RestRequest("http://www.dictionary.com/browse/computer", Method.GET);
   RestResponse response = client.Execute(request);
}

I am writing this code in D1Controller . For debugging I am using Fiddler. All the pre-written code is work well.

Your code won't compile since client.Execute(request) returns an object which implements the IRespResponse interface while you declared the response variable as of RestResponse type. There is no implicit conversion between this type and interface, so the compliler prevents you from making a logical error: the class RestResponse does implement IRestResponse , but it also may contain other fields and methods. You may safely cast from an interface to this type but not vice versa.

If you replace your declaration RestResponse response with IRestResponse response you will make your code compile and it actually works. However I'd recommend to examine C# basics

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