I have the following code to retrieve the HTML code from a given website:
private static string MakeRequest(string url)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
try
{
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
Stream response = resp.GetResponseStream();
StreamReader readStream = new StreamReader(response, Encoding.UTF8);
return readStream.ReadToEnd();
}
finally
{
resp.Close();
}
};
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
{
var resp = (HttpWebResponse)ex.Response;
if (resp.StatusCode = HttpStatusCode.NotFound)
{
return "N/A - URL = " + url;
}
if (resp.StatusCode = HttpStatusCode.GatewayTimeout)
{
return "N/A - Timed Out";
}
}
return;
}
I'm running Windows 10 and Visual Studio 12. The project is under a .NET Framework of 4.8.1. Researching this, all suggestions seem to be adding the two ServicePointManager lines as above and setting the SecurityProtocol to Tls12 as per the code above, but this has not made any difference.
Can anyone help me with this as prior to the most recent Visual Studio 12 update, this code was working fine without the two ServicePointManager lines?
Many Thanks Chris