I'm trying to build an application similar to Postman. Basically, the user is allowed to type in a URL and send different types of requests (GET, POST, etc.). For the beginning, I'm trying to implement GET and make sure it works properly.
I'm allowing the user to type in a URL, and a button click is going to trigger my RequestService.GetRequest() method, which looks like this:
public static async Task GetRequest(string url, RichTextBox ResponseRichTextBox, Label StatusCodeLabel)
{
string response = "";
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "C# program");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var resultContent = await client.GetStringAsync(url);
var resultStatusCode = await client.GetAsync(url);
Console.WriteLine("Output: " + resultContent.ToString());
ResponseRichTextBox.Text = resultContent.ToString();
if (resultStatusCode.IsSuccessStatusCode)
StatusCodeLabel.Parent.BackColor = Color.MediumSeaGreen;
else StatusCodeLabel.Parent.BackColor = Color.Crimson;
StatusCodeLabel.Parent.Show();
StatusCodeLabel.Text = resultStatusCode.StatusCode.ToString();
}
Everything seems to work pretty fine when sending requests to websites such as Google, or using API's such as GitHub's (https://api.github.com/users/.../repos). I'm properly receiving the data through the provided RichTextBox. The problem is when trying to send a request to Facebook. I've been trying to send it also via Postman and it seemed to work (it returned an HTML page), but using my app won't actually do that. It doesn't even output some message. Not even telling me that an exception occured.
I've been debugging the code and, while looking at the Output tab in Visual Studio, I saw this (immediately after sending a request to https://facebook.com):
Exception thrown: 'System.InvalidOperationException' in mscorlib.dll
I'm not sure, honestly, why this is happening. Thought that I missed out some headers, but I'm not sure if I'm correct (or what headers I should add/remove).