c# WebRequest to connect to wikipedia API

Posted by NickJ on Stack Overflow See other posts from Stack Overflow or by NickJ
Published on 2009-04-21T15:02:22Z Indexed on 2010/04/22 14:23 UTC
Read the original article Hit count: 898

Filed under:
|
|
|
|

Hey,

This may be a pathetically simple problem but I cannot seem to format the post webrequest/response to get data from the wikipedia api. I have posted my code below if anyone can help me see my problem.

string pgTitle = txtPageTitle.Text;

    Uri address = new Uri("http://en.wikipedia.org/w/api.php");

   HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";


    string action = "query";
    string query = pgTitle;

    StringBuilder data = new StringBuilder();
    data.Append("action=" + HttpUtility.UrlEncode(action));
    data.Append("&query=" + HttpUtility.UrlEncode(query));

    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());


    request.ContentLength = byteData.Length;

    using (Stream postStream = request.GetRequestStream())
    {
        postStream.Write(byteData, 0, byteData.Length);
    }

      using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        // Get the response stream   
        StreamReader reader = new StreamReader(response.GetResponseStream());


        divWikiData.InnerText = reader.ReadToEnd();
    }

© Stack Overflow or respective owner

Related posts about webrequest

Related posts about wikipedia