locked
Getting error 403 forbidden while calling wikipedia's Rest APIs RRS feed

  • Question

  • I am trying to fetch data from Wikipedia using Wikipedia Restful API.

    The code is throwing Web Exception. 403 Forbidden. 

    System.Net.WebException occurred
      HResult=-2146233079
      Message=The remote server returned an error: (403) Forbidden.
      Source=System
      StackTrace:
           at System.Net.HttpWebRequest.GetResponse()
           at wikiAPI.Program.Main(String[] args) in 
      InnerException: 

    Here is my C# code :-

    static void Main(string[] args)
            {
                try
                {
                    string wikiRequest = @"https://en.wikipedia.org/w/api.php?action=parse&format=json&page=Sachin_Tendulkar&prop=wikitext";
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(wikiRequest);
                    
                    myRequest.ContentType = "application/x-www-form-urlencoded";
                    myRequest.Accept = "*/*";
                    myRequest.UseDefaultCredentials = true;
                    myRequest.Method = "GET";
                    myRequest.Timeout=10000;
                    System.Net.ServicePointManager.Expect100Continue = false;
                    using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
                    {
                        string ResponseText;
                        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                        {
                            ResponseText = reader.ReadToEnd();
                        }

                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                }
            }

    Can any one tell me what am I doing wrong?

    Wednesday, January 2, 2013 7:31 AM

Answers

  • From https://www.mediawiki.org/wiki/API:Main_page

    "On Wikimedia wikis, if you don't supply a User-Agent header, or you supply an empty or generic one, your request will fail with an HTTP 403 error (cf. m:User-Agent policy). Other MediaWiki installations may have similar policies."

    So add

         myRequest.UserAgent = ... ... ...

    That make it work for me.


    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.


    Saturday, January 5, 2013 8:18 PM

All replies

  • I used wireshark to trace the internet connection while using your code.  I found the website is using TLS encryption.  See the following webpage for code to impliment TLS.

    http://www.codeproject.com/Articles/2642/SSL-TLS-client-server-for-NET-and-SSL-tunnelling


    jdweng

    Wednesday, January 2, 2013 10:37 AM
  • From https://www.mediawiki.org/wiki/API:Main_page

    "On Wikimedia wikis, if you don't supply a User-Agent header, or you supply an empty or generic one, your request will fail with an HTTP 403 error (cf. m:User-Agent policy). Other MediaWiki installations may have similar policies."

    So add

         myRequest.UserAgent = ... ... ...

    That make it work for me.


    http://www.alanjmcf.me.uk/ Please follow-up in the newsgroup. If I help, please vote and/or mark the question answered. Available for contract programming.


    Saturday, January 5, 2013 8:18 PM
  • Thanks Alan.

    I added the UserAgent string. It's working now.

    HarshJain

    Saturday, January 5, 2013 9:18 PM