Programatically posting data to a web form

J

Jeffery Tyree

All I would like is one example of working code that does nothing more than
submit a search criteria to Google and print to console the resulting page
of found items. To make it easier, below is my code. Simply correct it in
place so that I may copy/paste it back into the IDE and run it. Please do
not tell me to do net sniff comparisons or make sure I have the right
control values. If I knew these things I wouldn't be here asking, right?
Again, please simply correct my code in place so that it works and I (and
others) have an actual working example. Many many TIA.

FYI: the code is taken originally from the .NET Framework documentation

using System;
using System.Net;
using System.Text;
using System.IO;

class ClientGet
{
public static void Main(string[] args)
{
Uri site = new Uri("http://www.google.com");

WebRequest wReq = WebRequest.Create(site);

wReq.Method = "Post";
wReq.ContentType = "application/x-www-form-urlencoded" ;

string data = "q=starfish&btnG=click";
byte[] buffer = Encoding.UTF8.GetBytes (data);
wReq.ContentLength = buffer.Length ;

Stream reqStream = wReq.GetRequestStream ();
reqStream.Write (buffer, 0, buffer.Length);
reqStream.Flush ();
reqStream.Close ();

WebResponse wRes = wReq.GetResponse();
Stream respStream = wRes.GetResponseStream ();

StreamReader reader = new StreamReader(respStream, Encoding.ASCII);
String respHTML = reader.ReadToEnd();
Console.WriteLine(respHTML);
Console.ReadLine();

wRes.Close();
}

}
 
T

The Crow

use GET rather then post. this is how google is working. changing your uri
will be sufficient.
 
J

Jeffery Tyree

The Crow said:
use GET rather then post. this is how google is working. changing your uri
will be sufficient.

Thank you for the response, however, it did not help. In fact, simply
changing the method to "GET" as suggested yielded the following error:
==============
An unhandled exception of type 'System.Net.ProtocolViolationException'
occurred in system.dll
Additional information: Cannot send a content-body with this verb-type.
==============

-Jeff
 
T

The Crow

sory, you wouldnt post data at all. just request to URL and receive response
stream.
 

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