Я создал приложение, которое подключается к внешнему веб-сайту (вне моего контроля) к сервису мониторинга, который мы купили. Веб-сайт в основном представляет собой веб-API с графическим интерфейсом для машин, подобных поставщикам.
Из-за некоторых вещей я хотел сделать оповещения SMTP, если что-то пошло не так, поскольку наш поставщик еще не создал такую функциональность.
Мой код работает с HttpWebRequest
, и мое приложение открывает сайт, выполняет вход, успешно перенаправляет. Но когда я вызываю POST, это дает мне 500 ошибок HTTP в основном. Ошибка совершенно случайна, потому что иногда все может быть успешным и работать совершенно нормально, и просто через несколько минут не работает.
Часть моего кода:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim request As HttpWebRequest = CType(HttpWebRequest.Create("https://website.com"), HttpWebRequest)
Dim cookieContainer = New CookieContainer()
request.CookieContainer = cookieContainer
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
request.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
request.Host = "xxx"
request.Headers.Add("DNT", "1")
Dim Fullhtmlcode As String
Using response As WebResponse = request.GetResponse()
Using sr As New StreamReader(response.GetResponseStream())
Fullhtmlcode = sr.ReadToEnd
End Using
End Using
Dim SearchingString As String = "somestring"
Dim SearchingStringIndexINt As Integer = Fullhtmlcode.IndexOf(SearchingString)
Dim length As Integer = SearchingString.Length
Dim TokenArr() As Char = Fullhtmlcode.ToCharArray(SearchingStringIndexINt + length, 40)
Dim TokenString As String = New String(TokenArr)
request = TryCast(WebRequest.Create("https://website.com"), HttpWebRequest)
request.KeepAlive = True
request.CookieContainer = cookieContainer
request.Headers.Add("DNT", "1")
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.KeepAlive = True
request.AllowAutoRedirect = True
request.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
Dim Data As String = ("_token=" + TokenString + "&login=login&password=password")
Dim bytes As Byte() = UTF8Encoding.UTF8.GetBytes(Data)
request.ContentLength = bytes.Length
'login in
Using dataStream As Stream = request.GetRequestStream()
dataStream.Write(bytes, 0, bytes.Length)
dataStream.Close()
End Using
Using response As WebResponse = request.GetResponse()
Using sr As StreamReader = New StreamReader(response.GetResponseStream())
'Console.WriteLine(sr.ReadToEnd())
End Using
End Using
'get json data
Try
Dim requester As HttpWebRequest = CType(HttpWebRequest.Create("https://website.com/ajax/getMachines"), HttpWebRequest)
requester.Method = "POST"
requester.Host = "website.com"
requester.UserAgent = "Mozilla/5.0 (Windows NT 10.0 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
requester.Accept = "application/json, text/plain, */*"
requester.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
requester.ContentType = "application/json;charset=utf-8"
requester.KeepAlive = True
requester.CookieContainer = cookieContainer
requester.Referer = "https://website.com/"
Dim Uri = New Uri("https://website.com/ajax/getMachines")
'adding X-XSRF-TOKEN
For Each coookie In cookieContainer.GetCookies(Uri)
If coookie.ToString().Contains("XSRF-TOKEN") Then
Dim CookieLenght As Integer = coookie.ToString().Length
Dim c() As Char = coookie.ToString().ToCharArray(11, CookieLenght - 11)
Dim d As String = New String(c)
requester.Headers.Add("X-XSRF-TOKEN", c)
requester.Headers.Add("DNT", "1")
End If
Next
Data = "json data"
requester.ContentLength = Data.Length
bytes = UTF8Encoding.UTF8.GetBytes(Data)
requester.ContentLength = bytes.Length
Using dataStream As Stream = requester.GetRequestStream() 'calling json
dataStream.Write(bytes, 0, bytes.Length)
dataStream.Close()
End Using
Using responser As WebResponse = requester.GetResponse() 'random error 500
Using sr As StreamReader = New StreamReader(responser.GetResponseStream())
Console.WriteLine(sr.ReadToEnd())
End Using
End Using
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try