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(); }
You might want to try a GET request first because it’s a little simpler (you will only need to POST for wikipedia login). For example, try to simulate this request:
Here’s the code:
Edit: The other problem he was experiencing on the POST request was,
The exception is : The remote server returned an error: (417) Expectation failed.It can be solved by setting:(This is from: HTTP POST Returns Error: 417 "Expectation Failed.")