Reputation: 315
Below is my code for this HttpWebRequest. I was wondering why I can use this code on google.com or facebook.com.
While on this particular site which is below, I got this error The underlying connection was closed: An unexpected error occurred on a send.
What else do I need to add in my code to make this work? do you guys have an idea? I searched that I need certificate or trusted certificates to make an http request, but how can I implement this on my code?
protected void Button1_Click(object sender, EventArgs e)
{
WebRequest MyWebRequest;
WebResponse MyWebResponse;
StreamReader sr;
string strHTML;
StreamWriter sw;
MyWebRequest = HttpWebRequest.Create("https://qiita.com/naoki_mochizuki/items/3fda1ad6594c11d7b43c");
MyWebResponse = MyWebRequest.GetResponse();
sr = new StreamReader(MyWebResponse.GetResponseStream());
strHTML = sr.ReadToEnd();
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
sw = File.CreateText(@path +"\\"+TextBox1.Text+"");
sw.WriteLine(strHTML);
sw.Close();
}
Upvotes: 1
Views: 72
Reputation: 315
I added this code to solve this issue
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
Upvotes: 1
Reputation: 437
Make use of the CreateHttp static helper method instead of Create.
The reason for this is that you have a property ServerCertificateValidationCallback
which is a delegate that will let you allow to customize the action on certification validation.
In the case below, i have set it to statically return true to proceed or allow the request.
HttpWebRequest MyWebRequest;
WebResponse MyWebResponse;
StreamReader sr;
string strHTML;
StreamWriter sw;
MyWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://qiita.com/naoki_mochizuki/items/3fda1ad6594c11d7b43c");
MyWebRequest.ServerCertificateValidationCallback = (snd, cert, chain, err) => true;
Upvotes: 1