The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

ASP.NET (VB) 401 Error

Highlighted

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
Highlighted
Moderator

The problem is that your

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
Highlighted

Perfect, that was it.. thank

Perfect, that was it.. thank you.

Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured