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
I spent 2 days looking for this, so wanted to link the question I was asking, to the answer I was looking for:
The issue is that the link is
httpsand is secured by TLS 1.2, something not available by default in .NET 3.5This is answered here
Just in case the links die the solution is as follows:
Add imports:
VB
C#
Add before creating
HttpWebRequestVB
C#
For multiple protocols