Jump to content

VB6 to HTML/XML Post Method


geniusboy0

Recommended Posts

I have a functional XML POST from VB6 (provided by the developer of the server-side parser) that I am trying to adapt into an HTML based POST (javascript or anything I can use by just dropping a file into the webroot is ok). Here's the VB6:

strCmd = "<?xml version=""1.0"" encoding=""us-ascii"" ?>"strCmd = strCmd + " <Program.XML RPC=""" + Combo1.Text + """>"strCmd = strCmd + "   <Authentication>"strCmd = strCmd + "      <UserName>" + Text3.Text + "</UserName>"strCmd = strCmd + "      <Password>" + Text4.Text + "</Password>"strCmd = strCmd + "      <Domain>" + Text5.Text + "</Domain>"strCmd = strCmd + "      <Workstation>" + Text6.Text + "</Workstation>"strCmd = strCmd + "   </Authentication>"strCmd = strCmd + "   <Parameters>" + vbCrLfstrCmd = strCmd + "      <Identity>" + Text7.Text + "</Identity>" + vbCrLfstrCmd = strCmd + "   </Parameters>" + vbCrLfstrCmd = strCmd + "</Program.XML>"Inet1.Execute Text8.Text, "Post", strCmd, "Content-Type: text/xml"

The problem is that I do not have access to any feedback on an incorrect POST, meaning that I could have a formatting error in my XML and never get the result. When I run the VB6 code, everything works perfectly, so all I can try to do is exactly emulate what the Inet1.Execute command is doing. What I have been pounding away on is the following that seems to load without errors, but does not give me any results (I'm hoping this is a stupid format thing I'm missing at this point):

<html><head><script type="text/javascript">var xmlhttpfunction postXMLDoc(){var xmlData = '<?xml version="1.0" encoding="us-ascii" ?>' +' <Program.XML RPC="Door.MomentaryAccess">' +'   <Authentication>' +'      <UserName>userName</UserName>' +'      <Password>password</Password>' +'      <Domain>domain</Domain>' +'      <Workstation>serverName</Workstation>' +'   </Authentication>' +'   <Parameters>\r\n' +'      <Identity>8</Identity>\r\n' +'   </Parameters>\r\n' +'</Program.XML>';var url = 'http://serverName'xmlhttp=null// code for Mozilla, etc.if (window.XMLHttpRequest)  {  xmlhttp=new XMLHttpRequest()  }// code for IEelse if (window.ActiveXObject)  {  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")  }if (xmlhttp!=null)  {//Content-Type: text/xml//xmlhttp.setRequestHeader("Content-Type", "text/xml")  xmlhttp.onreadystatechange=state_Change  xmlhttp.open("POST",url,true)  xmlhttp.send(xmlData.xml)  alert(xmlData)  }else  {  alert("Your browser does not support XMLHTTP.")  }}function state_Change(){// if xmlhttp shows "loaded"if (xmlhttp.readyState==4)  {  // if "OK"  if (xmlhttp.status==200)  {  alert("XML data OK")  }  else  {  alert("Problem retrieving XML data:" + xmlhttp.statusText)  }  }}</script></head><body onLoad="postXMLDoc()"><h2>Please Work</h2></body></html>

One thought I have is that it might be related to the URL I'm using being over the local network "http://serverName". Do I need to hit this through a fully web-accessable address for these XMLHttpRequest() calls to work properly? I have tried it through both IE6/7 and Firefox. Any thoughts would be great, thanks.

Link to comment
Share on other sites

The URL for the XMLHttpRequest needs to be for the same server on which the script is running. So, if your page is running on http://someserver/ then the URL needs to be relative to that. The URL does not need to be publicly accessible. If you aren't getting any errors when you send the XMLHttpRequest then this probably isn't an issue.What might be happening is that the data that is being posted isn't formatted properly. It looks like you are simply sending the XML file. You might try sending the data in a key/value pair, something like this:

xmlhttp.send("xml=" + xmlData.xml);

I ran into a problem like this, and sending the data as "xml=<myxmldata>" rather than "<myxmldata>" seemed to please the server.

Link to comment
Share on other sites

Thanks for the reply, but...

xmlhttp.send("xml=" + xmlData.xml);

I ran into a problem like this, and sending the data as "xml=<myxmldata>" rather than "<myxmldata>" seemed to please the server.

Unfortunately, that didn't help at all with my problem and I still get the same non-results. I've tried taking out and putting in the \r\n stuff, but I haven't hand any luck with that either. I am worried the xmlhttp.send() command is not handled the same way as the POST from VB6, and I would have to somehow bundle the XML all at once with the URL.
Link to comment
Share on other sites

Oh, I forgot to mention that you also will want to set the Content-Type and Content-Length for the request. Some servers may not accept a POST request without those headers:

var data = "xml=" + xmlData.xml;xmlhttp.onreadystatechange=state_Change;xmlhttp.open("POST",url,true);xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");xmlhttp.setRequestHeader("Content-Length", data.length);xmlhttp.send(data);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...