Đây có thể là sự cố đơn giản về mặt đường dẫn, nhưng tôi dường như không thể định dạng yêu cầu/phản hồi web bài đăng để nhận dữ liệu từ Wikipedia API. Tôi đã đăng mã của mình dưới đây nếu có ai đó có thể giúp tôi xem vấn đề của mình.WebYêu cầu kết nối với Wikipedia API
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();
}
Thoạt nhìn bạn đang mã có vẻ tốt. Vấn đề cụ thể hiện diện như thế nào? Ngoại lệ là gì? –
Trường hợp ngoại lệ là: Máy chủ từ xa trả về lỗi: (417) Yêu cầu không thành công. – NickJ
@NickJ: Thử mã của tôi bên dưới. Nó đã làm việc. – Keltex