我试图在我的应用程序中连接一个api,此连接之前有效,但现在已停止。
如果我使用任何浏览器或邮递员应用程序,结果都是正确的,但是当我使用webrequest,webclient或Visual Studio中的任何方法时,我收到了超时错误。
我放置了用户代理,设置了超时时间,使用ssl,每次的结果都是相同的。
测试的终点是:
https://bittrex.com/api/v1.1/public/getmarkets
https://api.coinmarketcap.com/v1/ticker/?limit=1
当我使用类似google的网站进行测试时,此响应有效。
有什么主意吗?
使用WebRequestHelper更新
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports System.Text
Public Class WebRequestHelper
Private m_ResponseUri As Uri
Private m_StatusCode As HttpStatusCode
Private m_StatusDescription As String
Private m_ContentSize As Long
Private m_WebException As WebExceptionStatus
Public Property SiteCookies As CookieContainer
Public Property UserAgent As String = "Mozilla / 5.0(Windows NT 6.1; WOW32; Trident / 7.0; rv: 11.0) like Gecko"
Public Property Timeout As Integer = 30000
Public ReadOnly Property ContentSize As Long
Get
Return m_ContentSize
End Get
End Property
Public ReadOnly Property ResponseUri As Uri
Get
Return m_ResponseUri
End Get
End Property
Public ReadOnly Property StatusCode As Integer
Get
Return m_StatusCode
End Get
End Property
Public ReadOnly Property StatusDescription As String
Get
Return m_StatusDescription
End Get
End Property
Public ReadOnly Property WebException As Integer
Get
Return m_WebException
End Get
End Property
Sub New()
SiteCookies = New CookieContainer()
End Sub
Public Function GetSiteResponse(ByVal siteUri As Uri) As String
Dim response As String = String.Empty
ServicePointManager.DefaultConnectionLimit = 50
Dim maxFWValue As SecurityProtocolType = System.Enum.GetValues(GetType(SecurityProtocolType)).OfType(Of SecurityProtocolType)().Max()
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12 Or maxFWValue
ServicePointManager.ServerCertificateValidationCallback = AddressOf TlsValidationCallback
Dim Http As HttpWebRequest = WebRequest.CreateHttp(siteUri.ToString)
With Http
.Accept = "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
.AllowAutoRedirect = True
.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate
.CookieContainer = Me.SiteCookies
.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate")
.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.7")
.Headers.Add(HttpRequestHeader.CacheControl, "no-cache")
.KeepAlive = True
.MaximumAutomaticRedirections = 50
.ServicePoint.Expect100Continue = False
.ServicePoint.MaxIdleTime = Me.Timeout
.Timeout = Me.Timeout
.UserAgent = Me.UserAgent
End With
Try
Using webResponse As HttpWebResponse = DirectCast(Http.GetResponse, HttpWebResponse)
Me.m_ResponseUri = webResponse.ResponseUri
Me.m_StatusCode = webResponse.StatusCode
Me.m_StatusDescription = webResponse.StatusDescription
Dim contentLength As String = webResponse.Headers.Get("Content-Length")
Me.m_ContentSize = If(String.IsNullOrEmpty(contentLength), 0, Convert.ToInt64(contentLength))
Using responseStream As Stream = webResponse.GetResponseStream()
If webResponse.StatusCode = HttpStatusCode.OK Then
Dim reader As StreamReader = New StreamReader(responseStream, Encoding.Default)
Me.m_ContentSize = webResponse.ContentLength
response = reader.ReadToEnd()
Me.m_ContentSize = If(Me.m_ContentSize = -1, response.Length, Me.m_ContentSize)
End If
End Using
End Using
Catch exW As WebException
If exW.Response IsNot Nothing Then
Me.m_StatusCode = CType(exW.Response, HttpWebResponse).StatusCode
End If
Me.m_StatusDescription = "WebException: " & exW.Message
Me.m_WebException = exW.Status
End Try
Return response
End Function
Private Function TlsValidationCallback(sender As Object, CACert As X509Certificate, CAChain As X509Chain, SslPolicyErrors As SslPolicyErrors) As Boolean
If SslPolicyErrors = SslPolicyErrors.None Then Return True
Dim Certificate As New X509Certificate2(CACert)
CAChain.Build(Certificate)
For Each CACStatus As X509ChainStatus In CAChain.ChainStatus
If (CACStatus.Status <> X509ChainStatusFlags.NoError) And
(CACStatus.Status <> X509ChainStatusFlags.UntrustedRoot) Then
Return False
End If
Next
Return True
End Function
End Class
并使用它来调用课程
Dim url As String = "https://bittrex.com/api/v1.1/public/getmarkets"
Dim uri As New Uri(url)
Dim wr As New WebRequestHelper
Dim resp = wr.GetSiteResponse(uri)
答案 0 :(得分:0)
这是我如何连接到API的示例。
首先,根据您的JSON结果创建APIJSONViewModel,以存储id,名称,符号,等级,price_usd,price_btc等的值。
del kwargs['required']