For those struggling to (Basic) authenticate (as I have been…) with the Floorplanner API using ASP.NET, here’s a snip to get you going:
VB.NET:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | ' 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 |



May 25th, 2009 at 11:39 am
This is very good and informative