VB.NET HTTP Form Post

J

John Braham

I have a slight wall headbutter which I'm hoping someone will have an
idea for (I'd be very grateful!).

Basically I'm trying to make a form post to an asp page programatically
from within VB.NET 2005, the only data posted is one hidden field
XMLDOC, which contains an xml file which the receiving page will
respond to... I know, it's not a great way of doing this... I know I
should be using a web service, but basically this is the way I have to
interact with this page.

If I build an asp page to do this it works:

<HTML>
<HEAD></HEAD>
<BODY>

<form name="xConnectForm" method="POST"
action="http://campaigner.concepglobal.com/xconnect/xconnect.asp">

<input type="hidden" name="XMLDOC" value="<?xml
version='1.0'?><xconnect><interface><![CDATA[SERVER]]></interface><user><username><![CDATA[username]]></username><password><![CDATA[password]]></password></user><report><id>account_recipientactivity</id><input
name='datestart'>2006-10-27 20:50:00</input><input
name='dateend'>2006-11-27 20:55:00</input></report></xconnect>"/>

<input type="submit" />

</form>

</BODY>
</HTML>

Hitting submit on this form successfully causes the receiving page to
return the response, so what I've tried to do is translate this logic
into VB.NET code as follows:

' textbox1 has the url
' textbox2 has the request xml
' textbox3 is blank for seeing the response xml
Dim web As New System.Net.WebClient()
web.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

Dim d As Byte() = System.Text.Encoding.ASCII.GetBytes("XMLDOC=" &
TextBox2.Text)
Dim res As Byte() = web.UploadData(TextBox1.Text, "POST", d)

TextBox3.Text = "Response:" & System.Text.Encoding.ASCII.GetString(res)
& vbCrLf & vbCrLf
TextBox3.Text &= "Request:" & System.Text.Encoding.ASCII.GetString(d)

I've tried various other (longer and more complicated) examples of http
posting, all of which cause the reveiving page to fall over with an XML
parsing error (whereas the page process the earlier asp post example
correctly).

Now I'm assuming it's some kind of encoding issue, all the examples I
have encode the XML into the url string.

Basically what I'm asking is does anyone know of a way I can simulate
what I'm doing in the asp example, make a http post in the exact same
manner as if it was done from the browser?

Thanks,
John Braham
 
J

John Braham

Ok, partially answered my own question now, turns out (from setting up
a little test rig) that my encoding is to blame, it's stripping out all
the whitespace, which messes it up when the xml attribute names get
joined onto the node names!

Now all I have to figure out is how to encode it and not bugger up the
xml!

And my revised posting code is now:

Dim webClient As WebClient = New WebClient()
webClient.Headers.Add("Content-Type",
"application/x-www-form-urlencoded")
Dim response As Byte() = webClient.UploadData(TextBox1.Text,
"POST", Encoding.UTF8.GetBytes("XMLDOC=" & TextBox2.Text))

TextBox3.Text &= "Response: " & vbCrLf &
System.Text.Encoding.UTF8.GetString(response)

Which is just a tad shorter,

if anyone has any quick ideas on the whitespace issue I'd still be very
pleased to hear.

Thanks,
John Braham
 

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

Top