C# i WebRequest daje wynik 'Access Denied'

0

Witam,

do tej pory nie miałem okazji wykorzystywać klasy WebRequest w języku C#, a gdy już to zrobiłem to od razu problemy..

Mając taki oto kod:
http://rextester.com/IDOXC88093

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

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a request for the URL. 
            WebRequest request = WebRequest.Create (
              @"http://whatismyipaddress.com/ip/31.207.0.99");  // chcę konkretnie odpytać stronę whatismyipaddress.com
            // If required by the server, set the credentials.
            // Get the response.
            WebResponse response = request.GetResponse ();
            // Display the status.
            Console.WriteLine (((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Clean up the streams and the response.
            reader.Close ();
            response.Close ();
        }
    }
}

Wynik:
Access Denied (AK1). Contact [email protected]

Czyżby właściciel strony whatismyipaddress.com jakoś blokował requesty nie pochodzące z przeglądarki ? albo brakuje coś w kodzie..

0

Musisz ustawić UserAgent dla tego requesta. Mi to pomogło ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1