使用维基百科 api 时出错
我正在使用维基百科 API 从维基百科获取数据,但我在这里收到错误,我发布了我的代码和我的错误。请帮我。
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://en.wikipedia.org/w/api.php?action=opensearch&format=xml&search=hello");
System.Net.ServicePointManager.Expect100Continue = false;
using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
{
string ResponseText;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
ResponseText = reader.ReadToEnd();
}
lblresult.Text = ResponseText;
}
这是我的错误:
System.Net.WebException was unhandled by user code
Message=The remote server returned an error: (403) Forbidden.
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at _Default.btnsearch_Click(Object sender, EventArgs e) in c:\Users\Imran Ali\Desktop\Wikipedia\Default.aspx.cs:line 33
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
i am using wikipedia api to fetch data from wiki pedia but i am getting error here iam posting my code and my error. Please help me.
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://en.wikipedia.org/w/api.php?action=opensearch&format=xml&search=hello");
System.Net.ServicePointManager.Expect100Continue = false;
using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
{
string ResponseText;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
ResponseText = reader.ReadToEnd();
}
lblresult.Text = ResponseText;
}
Here is my error:
System.Net.WebException was unhandled by user code
Message=The remote server returned an error: (403) Forbidden.
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at _Default.btnsearch_Click(Object sender, EventArgs e) in c:\Users\Imran Ali\Desktop\Wikipedia\Default.aspx.cs:line 33
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要访问维基百科(无论是否使用 API),您必须在要求。这是因为维基媒体的用户代理政策。
将 User-Agent 标头设置为什么?引用上面链接的政策页面:
如何设置标题?使用
UserAgent
属性 在请求对象上。顺便说一句,对于像这样的简单请求,使用 更容易
WebClient
方法,例如DownloadString()
。To access Wikipedia (using the API or not), you have to set a User-Agent in the request. This is because of Wikimedia's User-Agent policy.
What to set the User-Agent header to? Quoting the policy page linked above:
How to set the header? Use the
UserAgent
property on the request object.As a side note, for simple requests like this, it's easier to use
WebClient
methods, for example,DownloadString()
.