VB.net sample code to add/update a subscriber
VB.net sample code to add or update a subscriber
Public Function AddSubscriber() As String Dim data As New StringBuilder() Dim ll_ret As String = "" Dim l_username As String = "USERNAME" Dim l_password As String = "PASSWORD" Dim l_listid As String = "LISTID" Dim l_email As String = "test@criticalimpact.com" Dim l_firstname As String = "Testfirst" Dim l_lastname As String = "Testlast" Try Dim l_posturl = "http://clients.criticalimpact.com/api/api.cfm" data.Append("c=") data.Append("<?xml version=""1.0""?>") data.Append("<criticalimpact>") data.Append("<login>") data.Append(" <username>" & l_username & "</username>") data.Append(" <password>" & l_password & "</password>") data.Append("</login>") data.Append(" <command>") data.Append(" <action>subscriberadd</action>") data.Append(" <listid>" & l_listid & "</listid>") data.Append(" <email><![CDATA[" & l_email & "]]></email>") data.Append(" <firstname><![CDATA[" & l_firstname & "]]></firstname>") data.Append(" <lastname><![CDATA[" & l_lastname & "]]></lastname>") data.Append(" <unsubscribe>0</unsubscribe>") data.Append(" <listunsubscribe>0</listunsubscribe>") data.Append(" <custom1>Sample Value</custom1>") data.Append(" <custom2>Sample Value</custom2>") data.Append(" <custom3>Sample Value</custom3>") data.Append(" <custom4>Sample Value</custom4>") data.Append(" </command>") data.Append("</criticalimpact>") 'NEW METHOD SO WE CAN SET A TIMEOUT ' Create a request using a URL that can receive a post. Dim request As WebRequest = WebRequest.Create(l_posturl) Dim l_response As String = "" ' Set the Method property of the request to POST. request.Method = "POST" ' Create POST data and convert it to a byte array. Dim byteArray As Byte() = Encoding.UTF8.GetBytes(data.ToString) ' Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded" ' Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length ' Get the request stream. Dim dataStream As Stream = request.GetRequestStream() ' Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length) ' Close the Stream object. dataStream.Close() ' Get the response. Dim response As WebResponse = request.GetResponse() ' Display the status. l_response = l_response & "StatusDescription:" & (CType(response, HttpWebResponse).StatusDescription) & vbCrLf ' Get the stream containing content returned by the server. dataStream = response.GetResponseStream() ' Open the stream using a StreamReader for easy access. Dim reader As New StreamReader(dataStream) ' Read the content. Dim responseFromServer As String = reader.ReadToEnd() ' Display the content. l_response = l_response & "ServerResponse:" & responseFromServer & vbCrLf ll_ret = responseFromServer.Trim() ' Clean up the streams. reader.Close() dataStream.Close() response.Close() Catch ex As System.Net.WebException 'Error in accessing the resource, handle it MsgBox("Error: " & Err.Description) End Try Return ll_ret End Function
Public Function AddSubscriber() As String
Dim data As New StringBuilder()
Dim ll_ret As String = ""
Dim l_username As String = "USERNAME"
Dim l_password As String = "PASSWORD"
Dim l_listid As String = "LISTID"
Dim l_email As String = "test@criticalimpact.com"
Dim l_firstname As String = "Testfirst"
Dim l_lastname As String = "Testlast"
Try
Dim l_posturl = "http://clients.criticalimpact.com/api/api.cfm"
data.Append("c=")
data.Append("<?xml version=""1.0""?>")
data.Append("<criticalimpact>")
data.Append("<login>")
data.Append(" <username>" & l_username & "</username>")
data.Append(" <password>" & l_password & "</password>")
data.Append("</login>")
data.Append(" <command>")
data.Append(" <action>subscriberadd</action>")
data.Append(" <listid>" & l_listid & "</listid>")
data.Append(" <email><![CDATA[" & l_email & "]]></email>")
data.Append(" <firstname><![CDATA[" & l_firstname & "]]></firstname>")
data.Append(" <lastname><![CDATA[" & l_lastname & "]]></lastname>")
data.Append(" <unsubscribe>0</unsubscribe>")
data.Append(" <listunsubscribe>0</listunsubscribe>")
data.Append(" <custom1>Sample Value</custom1>")
data.Append(" <custom2>Sample Value</custom2>")
data.Append(" <custom3>Sample Value</custom3>")
data.Append(" <custom4>Sample Value</custom4>")
data.Append(" </command>")
data.Append("</criticalimpact>")
'NEW METHOD SO WE CAN SET A TIMEOUT
' Create a request using a URL that can receive a post.
Dim request As WebRequest = WebRequest.Create(l_posturl)
Dim l_response As String = ""
' Set the Method property of the request to POST.
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(data.ToString)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
Dim dataStream As Stream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
l_response = l_response & "StatusDescription:" & (CType(response, HttpWebResponse).StatusDescription) & vbCrLf
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
l_response = l_response & "ServerResponse:" & responseFromServer & vbCrLf
ll_ret = responseFromServer.Trim()
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
Catch ex As System.Net.WebException
'Error in accessing the resource, handle it
MsgBox("Error: " & Err.Description)
End Try
Return ll_ret
End Function