我无法连接到2个URL中的1个的REST API
iContact.com的REST API有2个URL。
以下代码适用于第一个网址
“https://app.sandbox.icontact.com/icp/a/”
但是以下网址会失败
https://app.icontact.com/icp/a/
代码详情
错误: 底层连接已关闭:发送时发生意外错误。 SendFailure {4}
故障排除:
代码主模块:
Imports System.Net
Module MainModule
Sub Main()
Try
Dim MyRequest As HttpWebRequest
MyRequest = IContactManager.BuildJsonRequest
Dim iContactSecurityInformation As ContactClass
iContactSecurityInformation = IContactManager.GetiContactSecurityInformation(MyRequest)
Catch ex As Exception
End Try
End Sub
End Module
类别:
Imports Newtonsoft.Json.Linq
Imports System.Net
Imports System.IO
Imports System.Web.Script.Serialization
Imports System.Text
Public Class ContactClass
Public AppId As String
Public BaseUrl As String
Public Username As String
Public Password As String
Public AccountId As String
Public ClientFolderId As String
Public TestMode As Boolean
End Class
Public Class IContactManager
Public Shared Function BuildJsonRequest() As HttpWebRequest
Dim uri = New Uri(Convert.ToString(My.Settings.Base_URL + "a/"))
'Production
Dim request = DirectCast(WebRequest.Create(uri), HttpWebRequest)
request.Method = "Get"
request.Accept = "application/json"
request.ContentType = "application/json"
request.Headers.Add("Api-Version", "2.2")
request.Headers.Add("Api-AppId", "fakeID")
request.Headers.Add("Api-Username", "FakeUser")
request.Headers.Add("Api-Password", "FakePassword")
Return request
End Function
Public Shared Function GetiContactSecurityInformation(request As HttpWebRequest) As ContactClass
Dim Acct As New ContactClass
Dim a
Try
Using response = DirectCast(request.GetResponse(), HttpWebResponse)
Using reader = New StreamReader(response.GetResponseStream())
Dim jsonData = reader.ReadToEnd()
Dim serializer = New JavaScriptSerializer()
a = serializer.Deserialize(Of ContactClass)(jsonData)
End Using
End Using
Catch exc As System.Net.WebException
Dim webResponse = TryCast(exc.Response, System.Net.HttpWebResponse)
If webResponse IsNot Nothing AndAlso webResponse.StatusCode = System.Net.HttpStatusCode.Unauthorized Then
Console.WriteLine("401")
Else
Throw
End If
End Try
Return Acct
End Function
End Class
答案 0 :(得分:1)
我找到了答案,您只需要使用服务点管理器来处理无效的网址
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12