Between 9pm - 11pm ET this Sat (9/18), we will be doing routine network maintenance. Please avoid usage of our API during this time frame - including any scheduled sync jobs. Thank you in advance for your patience.

ASP.NET (VB) 401 Error

rockinthesixstring
Occasional Participant

ASP.NET (VB) 401 Error

Here is my code, please let me know what I'm missing


 HERE IS THE CODE


   Protected Sub lnkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkButton.Click

        Dim sUsername As String = ConstantContact.UserName

        Dim sPassword As String = ConstantContact.Password

        Dim sUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/activities"

        Dim sListUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/lists/1"

        Dim sAPIKey As String = ConstantContact.APIKey



        'Setup an HttpWebRequest

        Dim address As New Uri(sUri)

        Dim request As HttpWebRequest = TryCast(WebRequest.Create(address), HttpWebRequest)

        request.Credentials = New NetworkCredential((sAPIKey & "%" & sUsername), sPassword)

        request.Method = "POST"

        request.ContentType = "application/x-www-form-urlencoded"




        'Build data string

        Dim data As New StringBuilder()

        data.Append("activityType=" + HttpUtility.UrlEncode("ADD_CONTACTS", Encoding.UTF8))

        data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,First Name,Last Name" & Chr(10)), Encoding.UTF8))

        data.Append(HttpUtility.UrlEncode((txtSubscribe.Text & ", HTML, , "), Encoding.UTF8))

        data.Append("&lists=" + HttpUtility.UrlEncode(sListUri))



        Dim byteData As Byte() = UTF8Encoding.UTF8.GetBytes(data.ToString())

        Dim st As String = String.Empty



        request.ContentLength = byteData.Length

        Using postStream As Stream = request.GetRequestStream()

            postStream.Write(byteData, 0, byteData.Length)

        End Using



        Using response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)

            Dim reader As New StreamReader(response.GetResponseStream())

            st = reader.ReadToEnd()

        End Using

    End Sub


 


 


HERE IS THE ERROR


 



The remote server returned an error: (401) Unauthorized.


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.



Exception Details: System.Net.WebException: The remote server returned an error: (401) Unauthorized.



Source Error:











Line 62: End Using
Line 63:
Line 64: Using response As HttpWebResponse = TryCast(request.GetResponse(), HttpWebResponse)
Line 65: Dim reader As New StreamReader(response.GetResponseStream())
Line 66: st = reader.ReadToEnd()




Source File: C:\Development\Projects\wolftrapping.com\wwwroot\Default.aspx.vb    Line: 64


2 REPLIES 2
DaveBerard
Moderator

The problem is that your request URI is not sent over https.  You would need to use the correct URI for Authentication to work.  For more information on Authentication, please see here: developer.constantcontact.com/doc/authenticationBasic.


 


It should be as simple as changing:


Dim sUri As String = "http://api.constantcontact.com/ws/customers/" & sUsername & "/activities"


 


to


Dim sUri As String = "https://api.constantcontact.com/ws/customers/" & sUsername & "/activities"

Dave Berard
Senior Product Manager, Constant Contact
rockinthesixstring
Occasional Participant

Perfect, that was it.. thank you.

Developer Portal

View API documentation, code samples, get your API key.

Visit Page