I am trying to read contents from a webpage using .Net code inside a WinForm. This was a working code up until recently wherein I started receiving the below connection failure error.
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Now, upon investigating further, I find that the error from the code driven call is coming only between certain times of the day; from 9 AM IST to approximately 12 PM IST. However, during this time, the URL call fired from any browser returns the correct response. After 12 PM IST, the code driven call also starts working.
This has been now tested in multiple Windows environment - my personal desktop, laptop, amazon cloud based VPS. In each of these cases, the ISPs were different.
This makes me think that there may be a time-bound protocol switch at the server side which is not accepting requests during the specific time.
Option Strict On
Imports System.IO
Imports System.Net
Public Class frmMain
Private Async Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Await Task.Run(AddressOf StartProcessing)
End Sub
Private Async Function StartProcessing() As Task
Try
Dim responseCookies As New CookieContainer
Dim openPositionDataURL As String = "https://www.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp?symbolCode=-0&symbol=NIFTY&symbol=NIFTY&instrument=OPTIDX&date=-&segmentLink=17&segmentLink=17"
ServicePointManager.Expect100Continue = False
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
ServicePointManager.ServerCertificateValidationCallback = Function(s, Ca, CaC, sslPE)
Return True
End Function
Dim request As HttpWebRequest = WebRequest.CreateHttp(openPositionDataURL)
request.AllowAutoRedirect = True
request.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate Or DecompressionMethods.None
request.Host = "www.nseindia.com"
request.KeepAlive = True
request.Headers.Add("Upgrade-Insecure-Requests", "1")
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Trident/7.0; rv:11.0) like Gecko"
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
request.Headers.Add("Sec-Fetch-Site", "none")
request.Headers.Add("Sec-Fetch-Mode", "navigate")
request.Headers.Add("Accept-Encoding", "gzip, deflate, br")
request.Headers.Add("Accept-Language", "en-US,en;q=0.9,hi;q=0.8,ko;q=0.7")
request.CookieContainer = responseCookies
''request.ServicePoint.UseNagleAlgorithm = True
'request.ProtocolVersion = HttpVersion.Version10
'request.ServicePoint.ConnectionLimit = 12
'request.Proxy = Nothing
'request.ContentType = "application/x-www-form-urlencoded"
'request.Referer = "https://www.google.com/"
'request.Headers.Add("Sec-Fetch-User", "?1")
'request.ConnectionGroupName = Guid.NewGuid().ToString()
'request.Timeout = -1
Using wr As WebResponse = Await request.GetResponseAsync()
Using sr = New StreamReader(wr.GetResponseStream)
Console.WriteLine("URL Response")
End Using
End Using
Catch ex As Exception
Console.WriteLine(ex.Message)
Throw ex
End Try
End Function
End Class
The commented out code is what I have tried in addition after searching for similar problems in SO.
Here's the Wireshark dump when hit from Browser, vs when hit from the code.
https://1drv.ms/u/s!AvZ7cbmSuh_chfw3l142uZGjQrBt2A?e=iYzdEy
https://1drv.ms/u/s!AvZ7cbmSuh_chfw5DVMjckPdfBcBKA?e=pdWQ8q
How can I get this working between the times mentioned above?