I have been trying to do some http requests in C#. First time doing http request GET it worked, but the second time it didn't work and it returned null, can someone please help?
private static dynamic WebRequestGET(Uri url)
{
try
{
var request = WebRequest.Create(url);
request.Method = "GET";
request.Timeout = 10000;
WebResponse webResponse = request.GetResponse();
var webStream = webResponse.GetResponseStream();
var reader = new StreamReader(webStream);
var data = reader.ReadToEnd();
dynamic jsonData = JObject.Parse(data);
request.Abort();
webResponse.Close();
webStream.Close();
return jsonData;
}
catch(Exception e)
{
var window = GetConsoleWindow();
ShowWindow(window, 1);
Console.WriteLine($"Error occured while fetching data, if error will occur again please create issue on github{Environment.NewLine}{e.Message}");
Console.ReadKey();
Application.Exit();
return null;
}
}
Try using this to make the get request. You might need to add restsharp using nuget.
using RestSharp;
// ^^ put at top of file
var client = new RestClient("https://www.google.com");
var request = new RestRequest();
request.Method = Method.Get;
RestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
// change this to whatever you want ^^
I'd also recommend postman.com since it can generate the code for you.