Library tutorials & articles
POSTing Form Data to a Web Page
By Bruce Johnson, published on 19 Jan 2003
Introduction
Being able to post data to a web page from within a Visual Basic application can be quite handy. Even better when you can 'see' the response and process it automatically. Fortunately, there are at least two different techniques to achieve this goal. The best one depends on the environment in which your application will be deployed, as well as the kind of posting that you want to do. We'll start with the easiest approach and then move on to the more flexible solution.
Related articles
Related discussion
-
ditto
by zapthedingbat (2 replies)
-
Mousewheel
by jonh (3 replies)
-
True multithread VB source code controls
by James Crowley (3 replies)
-
Rely
by Yujvendra Verma (4 replies)
-
True multithread VB source code controls
by James Crowley (3 replies)
I am trying to post text data from my vb application to a web page field.how can i do this through simple asp code?help me
The easiest way to do this is by using the WebRobot component available at http://foxtrot-xray.com/web-robot/
This component handles normal forms, multipart forms, cookies, redirection, proxies, automatically for you, and makes development so much simpler. You can even upload files using this component.
Hello...
I have specific problem with ASP.NET server...
I have writen code in Delphi with WinInet - which collect binary data togehther with MultiPart Form variables and send them on to ASP.NET server... Form is received, but ASP.NET can not recognize it...
Begin of data look as this:
Content-Type: multipart/form-data; boundary=---------------------------7d63b9261906a2
Cookie: ASP.NET_SessionId=wyc1qgbe0bjkpmnv2eyn0m55; path=/
Content-Length: 924855
---------------------------7d63b9261906a2
Content-Disposition: form-data; name="Action"
New
---------------------------7d63b9261906a2
Content-Disposition: form-data; name="SMAPIAction"
FileUpload
---------------------------7d63b9261906a2
Content-Disposition: form-data; name="DraftID"
2C8C0EAD63B24D4492403BDF908DA67B
---------------------------7d63b9261906a2
Content-Disposition: form-data; name="file_0"; filename="C:\ROK-PLAYER.zip"
Content-Type: application/octet-string
PK...........
When I post these data to classic CGI under IIS, form data are recognized OK...
When I post them to ASP.NET server - problem continue...
I have set Content-Length of course...
Do you know some settings which can help me with posting data to ASP.NET server, please?
Thank you for responses...
I'm also looking for a solution to this problem. Has anyone else any answers? My problem being if you post back a string which contains an & - then what happens?
Thanks,
Chris
you have only to activate "microsoft xml 4.0" or "microsofr XML 5.0" and clik OK
I'm having the same problem, I'm trying to do the POST and redirect to PayPal site at the same time but the page is shown in my server.
Thanks in advance.
Luciano
I tried to get something out of the POST method by
Text1.Text = xmlhttp.ReponseText
but it says object's property/method not supported. also i can't find the ServerXMLHTTP class for MSXML2 in my libraries.. can anyone please tell me what is wrong with it..
musalpay,
Yes it works for me now. Perhaps if you explain what isn't working, others can help.
-jack
Excellent summary on how to POST content to a webserver.
I do have comment on the method that uses the ServerXMLHTTP. In the line
I would change it to something like
xmlhttp.send (PostStr)
Notice the parentheses around the variable PostStr. Without it, 0-byte length content is sent. Caused me a bit of grief as I tried to figure this out. Hopefully this comment will save others some time.
Are you solve the problem. It does not work. I dont understand why.
Has anyone been able to find the solution of the above problem?
if you have found the solution Bardyl , please email me at someone_chuss@yahoo.ca
How to programmatically send in Asp.Net a classic "multipart/form-data" form to a server?
Hi everyone ,
I've been searching for hours about a common Internet programming question and I'm certainly not the only one having the following problem :
With .Net (be it Delphi or VB), I need to POST a form to a .php server. If I pass the following parameter to the request :
objRequest.ContentType := 'application/x-www-form-urlencoded';
everything works fine. Except that if I need to transmit special characters from the form - like for example french or german accents - those laters are not transmitted in the format waited by the server (ISO-8859-1). So when you send text to the server via this form, you get unreadable or not corresponding character on the server side. Changing the content type like this on the *.aspx form source code doesn't change anything :
objRequest.ContentType := 'application/x-www-form-urlencoded; charset=ISO-8859-1';
No more result if you modify the web.config by adding the following line :
<globalization requestEncoding="ISO-8859-1" responseEncoding="ISO-8859-1" />
Another solution I tried was to HTML encrypt the parameter string posted back to server with the following .Net method :
FormString := HttpUtility.HtmlDecode(FormString);
No way. It makes new problem because of the "&" parameter separator... If you have special characters, it adds "&" in the string. For example the e-cute accent (é) is converted into é HTML code and the #233; is then considered as a parameter...
Some peolpe say that the content type "application/x-www-form-urlencoded" should not be used when you post a form. (there is also confusion between POST and GET method because usually you don't POST form be sending parameters into URL but this is another question). And I've remarked that actually, even if the server also accepts "application/x-www-form-urlencoded" form (certainly for compatibility reason), it is actually waiting for a "multipart/form-data" form. Posting form in "multipart/form-data" is also recommended by many people.
So, here is my question : how in Asp.Net to send programmatically to a server a classic "multipart/form-data" form containing characters of type ISO-8859-1 ? What must be the syntax of the string containing the content of the form and the request parameters ? With "application/x-www-form-urlencoded" form, the form parameter string is simple a line such as "parameter1=abc¶meter2=def". It doesn't work with "multipart/form-data" that encapsulate parameters between bounadaries. I found the following syntax exampe at http://www.faqs.org/rfcs/rfc2388.html but I don't succeed in applying it to my application :
--AaB03x
content-disposition: form-data; name="parameter1"
content-type: text/plain;charset=ISO-8859-1
content-transfer-encoding: quoted-printable
abc
--AaB03x--AaB03x
content-disposition: form-data; name="parameter2"
content-type: text/plain;charset=ISO-8859-1
content-transfer-encoding: quoted-printable
def
--AaB03x
There are plenty of information and examples about using the "multipart/form-data" form to upload file on a server but I couldn't find any about simply sending a classic form including text fields with special characters like accents. If one knows how to do that, it would be great if one could put a short example onto your site ! I think it could be very usefull for a lot of people !
Thanks a lot for your precious attention!
Laurent,
Switzerland
Hello,
I would like o use ServerXMLHTTP to POST data to a remote server and to also be redirected to that same page as if I am in a client browser and I clicked on the submit button of a form. I don't know how to get ServerXMLHTTP to follow the POST when I send it.
I understand that it returns a responseText object but when I write it out it displays a normal looking page but I am still sitting on my server rather than the server I tried POSTING too...
I'm hoping there are some properties that I can set to do this...
Any help would be much appreciated.
Here is my code that I've been playing around with:
<%@LANGUAGE="VBSCRIPT"%>
Dim objHttp
Dim str
If Request.ServerVariables("RequestMethod") = "POST" Then
str = "cmd=xclick&itemname=test&nonote=1¤cy_code=USD"
'Declare OUR POST object
Set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objHttp.Open "POST", "https://www.paypal.com/cgi-bin/webscr", FALSE
objHttp.setRequestHeader "Host", "www.paypal.com"
objHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHttp.setRequestHeader "Content-Length", Len(str)
objHttp.Send str
Response.Write (objHttp.ResponseText)
End If
Set objHttp=nothing%>
How do i post data contains carriage return character i.e., text from TEXTAREA control.
Maybe I'm just blind, but I've read through the API method a few times, and I can't see where in all that code you put the URL.
I can only find the path.
Excellent summary on how to POST content to a webserver.
I do have comment on the method that uses the ServerXMLHTTP. In the line
I would change it to something like
xmlhttp.send (PostStr)
Notice the parentheses around the variable PostStr. Without it, 0-byte length content is sent. Caused me a bit of grief as I tried to figure this out. Hopefully this comment will save others some time.
I am trying to get certain data from a GPS web page so that it can be transimitted to a microcontroller and later to a pda
any sugestions on what language I should use.
Thanks for this code sample, but I tried the VB code suggested, but my ASP page continues to say that no form data has been sent. I call my ASP page and send 1 parameter, then in my ASP page, I respond with this:
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
Response.Write "Form Parameters (Submit method=POST):"
For each reqItem in Request.Form
Response.Write reqItem & "=" & Request.Form(reqItem) & "<br>"
Next
However, no Request.Form items are written. Am I missing something?
Thanks!
How can this be done in C#? I tried to convert this solution and did not work.
It seems the object was not created successfully. Can anyone help out in this?
I didn't look at the code yet, but I wrote a program that catches machine data (through WMI and API) and posts it to a web site form. No browser opens up or anything, I only put a thank you message in the app to let the user know data has been sent. The purpose of the program is to keep an inventory of users hardware, etc. so that I know when to upgrade browsers/mail accounts/ HD etc. It writes to an access db that I look up. I've gotten the program to work on some 98/2k/xp Pro. Running the app on some 98 machines have had problems. If you want I can email you the whole package so that you can see how it works.
Anyone got the sample?
Also how to parse the data coming back from the POST ?
Guyz that was a wonderfull article, but now the other part is how do we process the data coming back as the response from the POST?
I wrote a very similar code using C++. The posted data is handled in the same page as the form. After posting the data, i get returned to the same page as though the data was not posted. Any ideas?
I am trying to post my vb data to a web page field. I have tried the code suggested with no success.
I have pasted in the declarations to my form generalities section. I created a command button and pasted the winlnet api code to the command button. When I press the command button, the processor kicks up, but I see no results. The web browser doesn't open and the desired web page never loads. I am not receiving any errors.
Have I missed something here.
Hello Bruce,
Could you please send the sample to me also at "paragm78@yahoo.com" ?
I want to post form variables to a webpage from a VB application.
Thanks.
Parag.
He articulates it is very good, the topic of the spaces that disappear is a problem, but has solution.
What I cannot solve is the transmission of characters like" &","%", etc.
Front Page, uses a shipment form that converts to hexadecimal these characters before sending them.
As this it is made?
Sorry for the delay in responding. There is a technique for doing this, but it is a little convoluted. It involves using a multipart request. I'll try to put together an example on the topic in the next couple of days and post it to the site.
The string needs to be converted to its URL encoding. You can find a routine to do this at http://www.tagconsulting.com/Show.asp?Id=1027.
You need to "escape" the line you are sending. You basically convert all non alpanumeric characters to their hex code equivalent. For example a space should be %20. Your line should be "Address=1%20Main%20Street".
There is a function in javascript that does this (escape and unescape). There are various routines for doing this in VB that I can point you to. Just let me know.
I agree. This article is excellent. Excatly what I was looking for!!
Brian
Great article! All works, except when I send a FORM field with white space it disappears.
So when objXMLHTTP.Send reqStr "Address=1 Main Street" is processed by the server, I get "1MainStreet" with white-space gone.
What am I doing wrong? Thanks!
Great article! All works, except when I send a FORM field with white space it disappears.
So when objXMLHTTP.Send reqStr "Address=1 Main Street" is processed by the server, I get "1MainStreet" with white-space gone.
What am I doing wrong? Thanks!
I'm having trouble implementing the <INPUT Type=file> posting method.
I implemented the first method easy, but when i tried the second it just doesn't work and i cant seem to find any help anyware about the subject.
Can you help me? I'm almost out of ideas.
Can you post the code of TestPost.asp page, so that i can see what in the hell am i doing wrong?
Thanks in advance!
EDIT: I removed the HTML Syntax from the subject line that was causing a few issues on the main page...
I actually do have a sample project that does this. Send me you're email address and I'll send it along.
This means that, if I have the same fields in a VB form, I can 'transfer' it into a HTML page?
Is there any working sample that I can download?
Well, it's brilliant...