收到错误(远程服务器返回错误:(401)未经授权)

时间:2013-06-19 07:53:18

标签: c# .net

我正在尝试使用API​​从LinkedIn获取评论,但我收到的错误如下所示

The remote server returned an error: (401) Unauthorized.

[WebException: The remote server returned an error: (401) Unauthorized.]
System.Net.HttpWebRequest.GetResponse() +6442408
Demo.WebForm1.Page_Load(Object sender, EventArgs e) in y:\Demo\Demo\WebForm1.aspx.cs:38
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean   includeStagesAfterAsyncPoint) +772

我已经在Page_Load中写了这段代码,我已经给出了请求

protected void Page_Load(object sender, EventArgs e) {
    string strURL = "http://api.linkedin.com/v1/groups/67657/posts?order=recency&category=discussion";
    System.Net.HttpWebRequest objWebRequest = null;
    System.Net.HttpWebResponse objWebResponse = null;
    System.IO.StreamReader streamReader = null;
    string strHTML = null;

    System.Net.ServicePointManager.ServerCertificateValidationCallback = 
        delegate(object s, 
                System.Security.Cryptography.X509Certificates.X509Certificate certificate, 
                System.Security.Cryptography.X509Certificates.X509Chain chain, 
                System.Net.Security.SslPolicyErrors sslPolicyErrors) 
                { return true; };

    objWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strURL);
    objWebRequest.Method = "GET";

    objWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;

    //Here I'm getting an error
    objWebResponse = (System.Net.HttpWebResponse)objWebRequest.GetResponse();

    streamReader = new System.IO.StreamReader(objWebResponse.GetResponseStream());

    strHTML = streamReader.ReadToEnd();


    streamReader.Close();
    objWebResponse.Close();
    objWebRequest.Abort();

    Response.ContentType = "text/xml";
    Response.Write(strHTML);
    Response.End();
}

0 个答案:

没有答案