Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference in programmatically filling in an online form versus posting to a Google-documents-generated form such as a Google Spreadsheet Live Form?

I tried the following with no success:

WebRequest req = WebRequest.Create(URI);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
string postData = "entry.0.single=ABCD&entry.1.single=EFGH&submit=click";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
req.ContentLength = byteArray.Length;
Stream os = req.GetRequestStream ();
os.Write (byteArray, 0, byteArray.Length); 
os.Close ();


and the response just gets me back to the Google form.
:confused:
Posted

I had alot of trouble with this, but after looking at the google code and sniffing the HTTP Post with Wireshark i was able to obtain this- a good starting point (but I was getting 405 errors on some of my more complicated test forms.. not sure why)


Imports System.Web
Imports System.Net
Imports System.IO
Imports System.Text


Public Class Form1
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim reader As StreamReader
    Dim address As Uri
    Dim appId As String
    Dim context As String
    Dim query As String
    Dim data As StringBuilder
    Dim byteData() As Byte
    Dim postStream As Stream = Nothing
    ''Dim myHttpUtility As HttpUtility

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        post()
    End Sub


    Sub post()

        address = New Uri("http://googleform")

        ' Create the web request  
        request = DirectCast(WebRequest.Create(address), HttpWebRequest)

        ' Set type to POST  
        request.Method = "POST"
        request.ContentType = "application/x-www-form-urlencoded"

        ' Create the data we want to send  
        appId = "ss-form"
        

        data = New StringBuilder()

        data.Append("entry.0.single=" + HttpUtility.UrlEncode(BrandBox.Text))
        data.Append("&entry.2.single=" + HttpUtility.UrlEncode(ModelBox.Text))
        data.Append("&entry.3.single=" + HttpUtility.UrlEncode(ModelStringBox.Text))

        ' Create a byte array of the data we want to send  
        byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())

        ' Set the content length in the request headers  
        request.ContentLength = byteData.Length

        ' Write data  
        Try
            postStream = request.GetRequestStream()
            postStream.Write(byteData, 0, byteData.Length)
        Finally
            If Not postStream Is Nothing Then postStream.Close()
        End Try

        Try
            ' Get response  
            response = DirectCast(request.GetResponse(), HttpWebResponse)

            ' Get the response stream into a reader  
            reader = New StreamReader(response.GetResponseStream())

            ' Console application output  
            MessageBox.Show(reader.ReadToEnd())
        Finally
            If Not response Is Nothing Then response.Close()
        End Try




    End Sub



End Class


-dwww.chromableedstudios.com[^]
 
Share this answer
 
v2
Comments
Dalek Dave 6-Dec-10 4:21am    
Good Answer.
Aravindba 21-Aug-20 4:48am    
this url give error "http://googleform", is it correct url ?
Hi
Thanks so much for looking into this. I'll give this a go when I get home.

I notice that the data string didn't include 'submit'? In googling for answers, I found some suggestions to include pageNumber backupCache and submit in the data string.

I'll try this out this evening.

Cheers
 
Share this answer
 
Comments
George Cane 11-Oct-17 7:24am    
Hi,

This looked like the answer to my problem. However, the code is fine until the line:

data.Append("entry.0.single=" + HttpUtility.UrlEncode(BrandBox.Text))

At which point it objects to 'HttpUtility' as not being declared.
It's declaration is 'commented out'... Twice !: (''Dim myHttpUtility As HttpUtility)
Removing the comments results in a 'Type HttpUtility is not defined' error.

'BrandBox' is also 'not declared' but I expect once the HttpUtility issue is fixed the answer to that will become apparent.


Poppa.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900