Search code examples
c#httpwebrequestfiddlerwebrequesthttpwebresponse

C# Http Web Request failing to certain sites


I trying to connect to awesemo.com using c# http request but its http connection failing by showing the following error: Error1: IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. Error2: SocketException: An existing connection was forcibly closed by the remote host.

I tried to access many other sites including Google.com all are worked fine but above website and one squarespace website connection failed. But one month before all were working fine. This is the new problem I facing. I tried lots in testing different codes as well as changed the framework but still unable to fix.

Here is my code: (All lines except the comment lines are copied from fiddler after a successful web request to above website using chrome browser.)

    private void test()
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.awesemo.com/");

        request.KeepAlive = true;
        request.Headers.Add("Upgrade-Insecure-Requests", @"1");
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
        //request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate, br");
        request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.9");
        request.Headers.Set(HttpRequestHeader.Cookie, @"_ga=GA1.2.523834838.1531216658; __stripe_mid=93850985-96a8-491e-b6ee-8c4b3147f3ab; _tccl_visitor=1fa9b919-5361-46be-9fb2-00cccf35c249");
        //System.Net.ServicePointManager.Expect100Continue = false;
        //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    }

Solution

  • After lots of testing I figure out the problem which was the .NET framework version. The framework version was 4.5 and after I updated it to 4.6 it solved my problem.