0

I'm attempting to to consume a XML webservice. The service is a asterix based PBX called switchvox. Each request should be in the form of XML, with XML being returned in the response. My code is follows, I am only able to get the API to return an error saying my request was empty.

Dim xml As String
xml = ""
xml = xml & " <request method=""switchvox.users.extensions.getInfo"">"
xml = xml & "     <parameters>"
xml = xml & "        <extensions>"
xml = xml & "         <extension>104</extension>"
xml = xml & "         </extensions>"
xml = xml & "        </parameters>"
xml = xml & "   </request>"
Dim url As String = "https://pbx/xml"
Dim webRequest__1 As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
webRequest__1.Method = "POST"
webRequest__1.Credentials = New NetworkCredential("user", "pass")
webRequest__1.ContentType = "text/xml"
webRequest__1.ContentLength = xml.Length
Using requestWriter2 As New StreamWriter(webRequest__1.GetRequestStream())
    requestWriter2.Write(xml)
End Using
Dim resp As HttpWebResponse = DirectCast(webRequest__1.GetResponse(), HttpWebResponse)
Dim responseData As String = String.Empty
Using responseReader As New StreamReader(webRequest__1.GetResponse().GetResponseStream())
    responseData = responseReader.ReadToEnd()
End Using
2
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Oct 12, 2014 at 20:38
  • Thank you for the correction, sorry for the mistake Commented Oct 13, 2014 at 15:21

1 Answer 1

1

Don't ever use string manipulation (including concatenation) to generate XML. Always use an XML API like LINQ to XML. You're lucky using VB.NET, in that you can use the XML Literals feature to build XML easily, and more likely correctly. Try something like this:

Public Function GetInfo() As XElement
    Dim xml As XElement = <request method="switchvox.users.extensions.getInfo">
                              <parameters>
                                  <extensions>
                                      <extension>104</extension>
                                  </extensions>
                              </parameters>
                          </request>

    Const url As String = "https://pbx/xml"
    Dim webRequest__1 As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
    webRequest__1.Method = "POST"
    webRequest__1.Credentials = New NetworkCredential("user", "pass")
    webRequest__1.ContentType = "text/xml"
    webRequest__1.ContentLength = xml.ToString().Length
    Using requestWriter2 As New StreamWriter(webRequest__1.GetRequestStream())
        requestWriter2.Write(xml.ToString())
    End Using
    Using resp As HttpWebResponse = DirectCast(webRequest__1.GetResponse(), HttpWebResponse)
        Using responseStream = resp.GetResponseStream()
            Return XElement.Load(responseStream)
        End Using
    End Using
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks the solution worked. I modified your code but i am having a hard time parsing the data correctly. Can you point me in the correct direction?
Ask a separate question with your parse problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.