Friday, 15 August 2014

vb.net - Consuming a XML webservice API -



vb.net - Consuming a XML webservice API -

i'm attempting to consume xml webservice. service asterix based pbx called switchvox. each request should in form of xml, xml beingness returned in response. code follows, able api homecoming error saying request empty.

dim xml 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 string = "https://pbx/xml" dim webrequest__1 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 new streamwriter(webrequest__1.getrequeststream()) requestwriter2.write(xml) end using dim resp httpwebresponse = directcast(webrequest__1.getresponse(), httpwebresponse) dim responsedata string = string.empty using responsereader new streamreader(webrequest__1.getresponse().getresponsestream()) responsedata = responsereader.readtoend() end using

don't ever utilize string manipulation (including concatenation) generate xml. utilize xml api linq xml. you're lucky using vb.net, in can utilize xml literals feature build xml easily, , more correctly. seek this:

public function getinfo() xelement dim xml xelement = <request method="switchvox.users.extensions.getinfo"> <parameters> <extensions> <extension>104</extension> </extensions> </parameters> </request> const url string = "https://pbx/xml" dim webrequest__1 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 new streamwriter(webrequest__1.getrequeststream()) requestwriter2.write(xml.tostring()) end using using resp httpwebresponse = directcast(webrequest__1.getresponse(), httpwebresponse) using responsestream = resp.getresponsestream() homecoming xelement.load(responsestream) end using end using end function

xml vb.net web-services

No comments:

Post a Comment