Asp.net 使用WikipediaAPI时出错

Asp.net 使用WikipediaAPI时出错,asp.net,api,wikipedia-api,Asp.net,Api,Wikipedia Api,我正在使用WikipediaAPI从wikipedia获取数据,但我在这里遇到了错误,我正在发布我的代码和错误。请帮帮我 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://en.wikipedia.org/w/api.php?action=opensearch&format=xml&search=hello"); System.Net.ServicePointManager.Expec

我正在使用WikipediaAPI从wikipedia获取数据,但我在这里遇到了错误,我正在发布我的代码和错误。请帮帮我

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未由用户代码处理
Message=远程服务器返回错误:(403)禁止。
来源=系统
堆栈跟踪:
在System.Net.HttpWebRequest.GetResponse()中
在c:\Users\Imran Ali\Desktop\Wikipedia\Default.aspx.cs第33行中的\u Default.btnsearch\u单击(对象发送者,事件参数e)
在System.Web.UI.WebControls.Button.OnClick(EventArgs e)中
位于System.Web.UI.WebControl.Button.RaisePostBackEvent(String eventArgument)
位于System.Web.UI.WebControl.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串事件参数)
位于System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)
位于System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)
内部异常:
要访问Wikipedia(无论是否使用API),您必须在请求中设置一个。这是因为

要将用户代理标头设置为什么?引用上面链接的政策页面:

脚本应使用带有联系信息的信息用户代理字符串,否则可能会在不通知的情况下被IP阻止

如何设置标题?在请求对象上使用


作为旁注,对于这样的简单请求,更容易使用方法,例如,
DownloadString()

服务器是否也在错误页面中返回任何文本?这可能会告诉你为什么得到403分。我没有收到403发出相同的API请求,所以他们可能阻止了你的IP。