使用维基百科 api 时出错

发布于 2024-12-28 20:17:07 字数 1574 浏览 1 评论 0原文

我正在使用维基百科 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2025-01-04 20:17:07

要访问维基百科(无论是否使用 API),您必须在要求。这是因为维基媒体的用户代理政策

将 User-Agent 标头设置为什么?引用上面链接的政策页面:

脚本应使用包含联系信息的信息丰富的用户代理字符串,否则它们可能会被 IP 封锁,恕不另行通知。

如何设置标题?使用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:

Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文