0
發生意外的錯誤,我無法連接到REST API爲1 2的網址.NET HttpWebRequest的:基礎連接已關閉:上一個發送
iContact.com有2個網址爲他們的REST API。
下面的代碼將在第一URL
「https://app.sandbox.icontact.com/icp/a/」
工作,但會失敗以下網址
https://app.icontact.com/icp/a/
代碼詳細
- 這是一個控制檯應用程序
- 應用程序寫在VB.net .Net框架4.5
- 的NuGet軟件包Newtonsoft.JSON
錯誤: 基礎連接已關閉:意外發送時發生錯誤。 SendFailure {4}
故障排除:
- 確認不使用鉻ARC應用測試休息APPI
- 能ping通網站
- 沙盒URL防火牆問題能夠連接並檢索結果
- 雖然提供的憑據是僞造的,但您應該能夠獲得證書錯誤而不是連接關閉
代碼主要模塊:
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