プログラムで Http リクエストを作成し、レスポンスを取得できます。
string uri = "http://www.google.com/search";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
// encode the data to POST:
string postData = "q=searchterm&hl=en";
byte[] encodedData = new ASCIIEncoding().GetBytes(postData);
request.ContentLength = encodedData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(encodedData, 0, encodedData.Length);
// send the request and get the response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Do something with the response stream. As an example, we'll
// stream the response to the console via a 256 character buffer
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Char[] buffer = new Char[256];
int count = reader.Read(buffer, 0, 256);
while (count > 0)
{
Console.WriteLine(new String(buffer, 0, count));
count = reader.Read(buffer, 0, 256);
}
} // reader is disposed here
} // response is disposed here
もちろん、Google は検索クエリに POST ではなく GET を使用するため、このコードはエラーを返します。
URL と POST データはすべて基本的にハードコーディングされているため、この方法は特定の Web ページを扱っている場合に有効です。もう少し動的なものが必要な場合は、次のようにする必要があります。
- ページをキャプチャする
- フォームを剥がす
- フォーム フィールドに基づいて POST 文字列を作成する
FWIW、Perl や Python のようなものがその種のタスクにより適していると思います。
編集: x-www-form-urlencoded