XML Trouble Moving to .Net

D

DouglasABaker

I'm having problems getting this to return a response in .Net, any
ideas? I've been playing around with WebRequest/WebResponse, is that
what I should use? Thanks

sXMLMessage = "<?xml version=" & Chr(34) & "1.0" & Chr(34) & "?>" &
vbCrLf
sXMLMessage = sXMLMessage & "<transaction>"
sXMLMessage = sXMLMessage & "<userid>" & sUserId & "</userid>"
sXMLMessage = sXMLMessage & "<password>" & sPassword & "</password>"
sXMLMessage = sXMLMessage & "</transaction>"

'Send HTTP request
set objHTTP = CreateObject("MICROSOFT.XMLHTTP")
Set objXml =CreateObject("Microsoft.XMLDOM")

Call objHTTP.open("POST", kXMLhostURL & ".asp", False)
Call objHTTP.send(sXMLMessage)
If Ucase(objHTTP.statusText) = "OK" Then
If InStr(Lcase(objHTTP.responseText), "xml") Then
objXml.loadXml(objHTTP.responsetext)
If objXml.parseError.errorCode = 0 Then

Set objNode =
objXml.selectSingleNode("//transaction_result//status_code")
if not objNode is nothing then
sStatusCode = objNode.text
end if
set objNode = nothing
etc...
 
M

Martin Honnen

I'm having problems getting this to return a response in .Net, any
ideas? I've been playing around with WebRequest/WebResponse, is that
what I should use?

Yes, and use XmlTextWriter to create the XML, don't use string
concatenation.
If you need to manipulate the returned XML then use an XmlDocument, if
you only want to read out values you can use XPathDocument.
 
D

DouglasABaker

How do I load the XMLTextWriter into the WebRequest?

Dim myXmlTextWriter As XmlTextWriter

myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartDocument(False)
myXmlTextWriter.WriteStartElement("transaction")
myXmlTextWriter.WriteElementString("userid", sUserId)
myXmlTextWriter.WriteElementString("password", sPassword)
myXmlTextWriter.WriteEndElement()


Dim myRequest As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create("http://***.*****.com/******.asp")
Dim myResponse As System.Net.HttpWebResponse =
myRequest.GetResponse()
Dim myReader As System.Xml.XmlTextReader = New
System.Xml.XmlTextReader(myResponse.GetResponseStream())
Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument
doc.Load(myReader)
 
D

DouglasABaker

I figured it out in VB if anybody else needs it. Feels good when it
works.

Dim myReq As HttpWebRequest =
CType(WebRequest.Create("http://***.******.com/********.asp"),
HttpWebRequest)
myReq.Method = "POST"

Dim myXmlTextWriter As XmlTextWriter = New
XmlTextWriter(myReq.GetRequestStream(), System.Text.Encoding.UTF8)

myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartDocument(False)
myXmlTextWriter.WriteStartElement("transaction")
myXmlTextWriter.WriteElementString("userid", sUserId)
myXmlTextWriter.WriteElementString("password", sPassword)
myXmlTextWriter.WriteEndElement()

Dim doc As XmlDocument = New XmlDocument
doc.WriteTo(myXmlTextWriter)
myXmlTextWriter.Close()

Dim myResp As HttpWebResponse
myResp = CType(myReq.GetResponse, HttpWebResponse)
Dim MyReader As StreamReader = New
StreamReader(myResp.GetResponseStream)
Dim strResult
strResult = MyReader.ReadToEnd
MyReader.Close()
Response.Write(strResult)
 
M

Martin Honnen

Dim myReq As HttpWebRequest =
CType(WebRequest.Create("http://***.******.com/********.asp"),
HttpWebRequest)
myReq.Method = "POST"

Dim myXmlTextWriter As XmlTextWriter = New
XmlTextWriter(myReq.GetRequestStream(), System.Text.Encoding.UTF8)

myXmlTextWriter.Formatting = System.Xml.Formatting.Indented
myXmlTextWriter.WriteStartDocument(False)
myXmlTextWriter.WriteStartElement("transaction")
myXmlTextWriter.WriteElementString("userid", sUserId)
myXmlTextWriter.WriteElementString("password", sPassword)
myXmlTextWriter.WriteEndElement()

The above looks fine but why the next two lines?
Dim doc As XmlDocument = New XmlDocument
doc.WriteTo(myXmlTextWriter)

You create a new empty XmlDocument and write that to the XmlTextWriter?
Why do think you need that?
I would simply expect you to call e.g.
myXmlTextWriter.WriteEndDocument()
and then close the writer as you do
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top