I am trying to unsubscribe our clients from our email list when they enter information into a form on our website. It’s a VB.Net application written in Visual Studio 2005.
The pertinent bit of code is this: (DIMS done earlier in the code for this bit)
If Me.TextBox1.Text > "" Then
returnxml = DRM.CheckCCEmail(Me.TextBox1.Text.ToString)
mdoc = New XmlDocument
mdoc.LoadXml(returnxml)
Dim namemngr As New XmlNamespaceManager(mdoc.NameTable)
namemngr.AddNamespace("client", "http://www.w3.org/2005/Atom")
namemngr.AddNamespace("contact", "http://ws.constantcontact.com/ns/1.0/ ")
Dim Root As XmlNode = mdoc.SelectSingleNode("client:feed", namemngr)
mnode = mdoc.SelectSingleNode("//client:entry/client:content/contact:Contact/contact:Status", namemngr)
Try
If mnode.InnerText.Contains("Active") Then
mnode.InnerText = "Do Not Mail"
'mdoc.DocumentElement("//client:entry/client:content/contact:Contact/contact:Status").InnerText = "Do Not Mail"
'mnode = mdoc.SelectSingleNode("//client:entry/client:content/contact:Contact/contact:Status", namemngr)
mdoc.WriteTo(xw)
returnxml = sw.ToString
FinalResult = DRM.ExecuteCCUrl(Me.TextBox1.Text.ToString, returnxml)
End If
Catch ex As Exception
Dim error1 As String = ex.Message
Me.Label1.Visible = True
End Try
End If
Everything works fine when I retrieve the client’s info. I can change the status to Do Not Mail from Active no problem.
However when I try to post that information back to the constant contact api, I invariably get a 401 not authorized error.
I’m talking to the API on both sides of this and supplying authorization credentials the same way on both sides so I’m baffled why my requests would be authorized in the first call of the API but not authorized in my subsequent post of the same data (simply modified to show Do Not Mail as the status) back to the API. I’m given to understand this is the preferred method of effecting changes.
I’ve included both calls to the api below so you can see that I am basically using the same methods both times except that the first time I’m issuing a GET and the second call is a PUT operation.
I’ve replaced our APIkey, username and password data with XXXX but I can assure you they are the same in both pieces of code when they get executed.
These are the bits of the DRM object that gets called above that handle talking to the API.
Public Shared Function CheckCCEmail(ByVal emailadd As String) As String
Dim apikey As String = "XXXXXX"
Dim UName As String = "XXXXX"
Dim UPass As String = "XXXXX"
Dim Email As String = HttpUtility.UrlEncode(LCase(emailadd))
Dim uri As New Uri("https://api.constantcontact.com/ws/customers/ " & UName & "/contacts?email=" & Email)
Dim LoginCredentials As CredentialCache = New CredentialCache
LoginCredentials.Add(New Uri("https://api.constantcontact.com/ws/customers/ " & UName), "Basic", New NetworkCredential((apikey + ("%" + UName)), UPass))
If (uri.Scheme = uri.UriSchemeHttps) Then
Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = WebRequestMethods.Http.Get
request.Credentials = LoginCredentials
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
response.Close()
Return tmp
End If
End Function
Public Shared Function ExecuteCCUrl(ByVal emailadd, ByVal xmldata) As String
Dim apikey As String = "XXXXX"
Dim Uname As String = "XXXX"
Dim UPass As String = "XXXX"
Dim Email As String = HttpUtility.UrlEncode(LCase(emailadd))
Dim uri As New Uri("https://api.constantcontact.com/ws/customers/ " & Uname & "/contacts?email=" & Email)
Dim LoginCredentials As CredentialCache = New CredentialCache
LoginCredentials.Add(New Uri("https://api.constantcontact.com/ws/customers/ " & Uname), "Basic", New NetworkCredential((apikey + ("%" + Uname)), UPass))
If uri.Scheme = uri.UriSchemeHttps Then
Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
request.ContentLength = xmldata.Length
request.ContentType = "application/x-www-form-urlencoded"
request.Method = WebRequestMethods.Http.Put
Dim writer As New StreamWriter(request.GetRequestStream)
writer.Write(xmldata)
writer.Close()
Dim oResponse As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(oResponse.GetResponseStream())
Dim tmp As String = reader.ReadToEnd()
oResponse.Close()
Return tmp
Else
Return "Fail"
End If
End Function
Thanks in advance for any help.
Steve Wells
Programmer and Data Base Administrator
Described and Captioned Media Program
... View more