Reputation: 2534
I am unable to connect to REST API for 1 of 2 URLs
iContact.com has 2 URLS for their REST API.
The following code will work for the first URL
"https://app.sandbox.icontact.com/icp/a/"
But will fail for the following URL
https://app.icontact.com/icp/a/
Code Details
Error: The underlying connection was closed: An unexpected error occurred on a send. SendFailure{4}
Troubleshooting:
Code Main Module:
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
Class:
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
Upvotes: 0
Views: 3259
Reputation: 2534
I found the answer, you just need to use service point manager for the URL that is not working
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Upvotes: 1