Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

Como fazer uma requisição do tipo GET

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    C# Como fazer uma requisição do tipo GET

    Segue o exemplo todo comentado, é só copiar e se precisar, fazer alguns ajustes para adequar às suas necessidades.

    Testado e funcionando com .NET 2.0
    Código PHP:
    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 parametros pelo método post 
                
    GetPage("http://www.guiadohacker.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(responseStreamencoding);

                    
    // Aloca o buffer
                    
    Char[] buffer = new Char[256];
                    
    int count reader.Read(buffer0buffer.Length);

                    
    // Imprime na  tela o que recebi
                    
    while (count 0)
                    {
                        
    Console.Write(new String(buffer0count));
                        
    count reader.Read(buffer0buffer.Length);
                    }

                    
    Console.WriteLine("");
                }
                catch (
    Exception e)
                {
                    
    Console.WriteLine("ERRO: " e.Message);
                }
                finally
                {
                    
    // Fecho tudo
                    
    if (response != null)
                        
    response.Close();
                    if (
    reader != null)
                        
    reader.Close();
                }
            }
        }

    ~# Criado pela [IN]Segurança #~
X
Working...
X