-
Notifications
You must be signed in to change notification settings - Fork 4
ASP.NET classes and examples
timknip edited this page Sep 13, 2010
·
1 revision
' This snip shows how to add a new user,
' see floorplanner.com/api for details
Dim uri As Uri = new Uri(ADD_USER_URL)
Dim request as HttpWebRequest = CType(WebRequest.Create(uri),HttpWebRequest)
' Build XML for the POST body (in this case from a custom User class)
Dim xmlStr As String = User.ToXmlString()
' Grab the bytes
Dim xmlBytes As Byte() = Encoding.UTF8.GetBytes(xmlStr)
request.Method = "POST"
request.ContentLength = xmlBytes.Length
request.ContentType = "application/xml"
' Basic Authorization
' Make sure PreAuthenticate is set to True, else the
' Authorization header will *not* be sent, and an error will be thrown
request.Credentials = New NetworkCredential(API_KEY, API_PASS)
request.PreAuthenticate = True
' Alternatively use a more old-fashioned method
' Dim basicAuth As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(API_KEY & ":" & API_PASS))
' request.Headers.Add("Authorization", "Basic " & basicAuth)
' Add our payload
Dim reqStream As Stream = request.GetRequestStream
reqStream.Write(xmlBytes, 0, xmlBytes.Length)
reqStream.Close
Try
Dim resp As HttpWebResponse = CType(request.GetResponse(),HttpWebResponse)
Dim reader As StreamReader = New StreamReader(resp.GetResponseStream)
' do something with the result XML string
Dim userXml As String = reader.ReadToEnd
Catch e As WebException
' handle the error
End Try