WebRequest, Response & XML

Zamdrist

Member
Joined
May 21, 2010
Messages
13
Programming Experience
5-10
Working on a program that makes use of a third party API. That API can be found here: API v2 [AllardSoft Documentation]

I've gone to the vendor, and the vendor unfortunately can only be of so much help. The API they published does work and I've been able to verify it works via a *nix korn shell, using Curl, just like the examples they give.

I really should be able to do the same via VB.Net.

The code below executes without error if I change the ContentType to "plain/text". If I change it to "text/xml" I get an error: 401 (Unauthorized). Unfortunately it's not doing what it really needs to do, post the supplied XML string, and get in return (response) a new string of XML text that, according to the API, is the api_key value. I understand that unless you too have this FileTransfer Applicance, it may be hard to answer my question. However, I don't think the API does anything that many other service oriented web sites do, i.e. accept an xml stream, and respond back in an xml stream.

Any ideas where I might be going wrong? What I ultimately need to do is read the response as XML, iterate the key values in that response, and store the value in a variable for later use. Thanks.

        Dim r As StreamReader
        Dim result As String
        Dim h As HttpWebRequest = WebRequest.Create("https://filetransfer.somewhere.com")
        Dim xml As String = "<?xml version=""1.0"" encoding=""UTF-8""?><user><email>someone@somewhere.com</email><password>somepassword</password></user>"
        Dim enc As New UTF8Encoding
        Dim byte1 As Byte() = enc.GetBytes(xml)

        h.Method = "POST"
        h.ContentType = "text/xml"
        h.ContentLength = byte1.Length

        Dim st As Stream = h.GetRequestStream()
        st.Write(byte1, 0, byte1.Length)
        st.Close()

        Dim response As WebResponse = h.GetResponse()
        r = New StreamReader(response.GetResponseStream)

        result = r.ReadToEnd
        txtResponse.Text = ""
        txtResponse.Text = result
        response.Close()
 
One mistake I see I was making was, I need to sent my request to https://filetransfer.somewhere.com/login

Yet now, I get: (406) Not Acceptable

That alludes to the content type.

HTTP/1.1: Status Code Definitions

10.4.7 406 Not Acceptable
The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

Unless it was a HEAD request, the response SHOULD include an entity containing a list of available entity characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. Depending upon the format and the capabilities of the user agent, selection of the most appropriate choice MAY be performed automatically. However, this specification does not define any standard for such automatic selection.

Note: HTTP/1.1 servers are allowed to return responses which are
not acceptable according to the accept headers sent in the
request. In some cases, this may even be preferable to sending a
406 response. User agents are encouraged to inspect the headers of
an incoming response to determine if it is acceptable.
If the response could be unacceptable, a user agent SHOULD temporarily stop receipt of more data and query the user for a decision on further actions.
 
Back
Top