Fazendo uma requisição do tipo Get em ASP.NET
O exemplo esta todo comentado tendo apenas que adaptar as suas necessidades. Este é bem mais simples pois estou enviando uma requisição do tipo GET e não precisa ter atribuído nenhum ContentType à página.
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Web; //não esqueça de fazer referência a dll em seu projeto
namespace Microsoft.Samples.QuickStart.HowTo.Net.WebRequests
{
static class ClientPOST
{
public static void main(String[] args)
{
//Enviando para a página do terra os parâmetros pelo método post
GetPage("http://www.terra.com.br");
Console.WriteLine( );
Console.WriteLine("Pressione algo para continuar...");
Console.ReadLine( );
}
private static void GetPage(String url)
{
WebResponse response = null;
StreamReader reader = null;
try
{
//cria o request da url
WebRequest request = WebRequest.Create(url);
//recebe os dados
response = request.GetResponse( );
Stream responseStream = response.GetResponseStream( );
//Exibe os caracteres especiais
System.Text.Encoding encoding = System.Text.Encoding.Default;
Reader = new StreamReader(responseStream, encoding);
//Aloca o buffer
Char[] buffer = new Char[256];
Int count = reader.Read(buffer, 0, buffer, length);
// Imprime na tela o que recebi
while (count > 0)
{
Console.Write(new String(buffer, 0, count));
Count = reader.Read(buffer, 0, buffer, Length);
}
Console.WriteLine("");
}
catch (Exception e)
{
Console.WriteLine("ERRO: " + e.Message);
}
finally
{
//Fecho tudo
if (response ! = null)
response.Close( );
if (reader ! = null)
reader.Close( );
reader.Close( );
}
}
}
}