I'm attempting to call an api using .NET Framework 3.5
I'm trying it with HttpWebRequest
but I've not had different results with WebClient
The following code works in .NET Framework 4.8, however in .NET Framework 3.5 there is a WebException
thrown from GetResponse()
Code:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://abolutepathtoendpoint.aspx");
req.ContentType = "text/plain";
req.Method = "GET";
req.ContentLength = 0;
string respBody;
try
{
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
{
respBody = reader.ReadToEnd();
}
}
catch (Exception e)
{
respBody = e.ToString();
}
Exception:
The remote server returned an error: (403) Forbidden.
I have also been able to get it to work by pasting the url into a brower, Postman and RestClient on .NET 7