当我尝试访问yahoo API时出现Http错误406

时间:2014-09-17 12:42:42

标签: http yahoo yahoo-api yahoo-oauth

我使用yahoo oAuth1和yahoo API从雅虎服务器获取联系人列表。

这里是我用来获取联系人的代码:

private void RetriveContacts()
        {
            OAuthBase oauth = new OAuthBase();
            Uri uri = new Uri("https://social.yahooapis.com/v1/user/" + (string)Session["xoauth_yahoo_guid"] + "/contacts?format=XML");
            string nonce = oauth.GenerateNonce();
            string timeStamp = oauth.GenerateTimeStamp();
            string normalizedUrl;
            string normalizedRequestParameters;
            string sig = oauth.GenerateSignature(uri, ConsumerKey, ConsumerSecret,
                                                 (string)Session["oauth_token"], (string)Session["oauth_token_secret"], "GET",
                                                 timeStamp, nonce, OAuthBase.SignatureTypes.HMACSHA1,
                                                 out normalizedUrl, out normalizedRequestParameters);
        StringBuilder sbGetContacts = new StringBuilder(uri.ToString());

        try
        {
            string returnStr = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sbGetContacts.ToString());
            req.Method = "GET";

            string authHeader = "Authorization: OAuth " +
            "realm=\"yahooapis.com\"" +
            ",oauth_consumer_key=\"" + ConsumerKey + "\"" +
            ",oauth_nonce=\"" + nonce + "\"" +
            ",oauth_signature_method=\"HMAC-SHA1\"" +
            ",oauth_timestamp=\"" + timeStamp + "\"" +
            ",oauth_token=\"" + (string)Session["oauth_token"] + "\"" +
            ",oauth_version=\"1.0\"" +
            ",oauth_signature=\"" + HttpUtility.UrlEncode(sig) + "\"";

            req.Headers.Add(authHeader);

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader streamReader = new StreamReader(res.GetResponseStream());
            returnStr = streamReader.ReadToEnd();

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(returnStr);
            XmlNodeList elemList = xmldoc.DocumentElement.GetElementsByTagName("fields");

            List<string> emails = new List<string>();
            for (int i = 0; i < elemList.Count; i++)
            {
                if (elemList[i].ChildNodes[1].InnerText == "email")
                    emails.Add(elemList[i].ChildNodes[2].InnerText);
                //Response.Write(elemList[i].ChildNodes[2].InnerText + "<br/>");
            }

        }
        catch (WebException ex)
        {
            //Response.Write(ex.Message);
            Response.Write("<br/>" + ex.Message + "</br>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            Response.Write("<br/>length: " + ex.Source.Length.ToString());
            Response.Write("<br/>stack trace: " + ex.StackTrace);
            Response.Write("<br/>status: " + ex.Status.ToString());
            HttpWebResponse res = (HttpWebResponse)ex.Response;
            int code = Convert.ToInt32(res.StatusCode);

            Response.Write("<br/>Status Code: (" + code.ToString() + ") " + res.StatusCode.ToString());
            Response.Write("<br/>Status Description: " + res.StatusDescription);

            if (ex.InnerException != null)
            {
                Response.Write("<br/>innerexception: " + ex.InnerException.Message);
            }

            if (ex.Source.Length > 0)
                Response.Write("<br/>source: " + ex.Source.ToString());

            if (res != null)
            {
                for (int i = 0; i < res.Headers.Count; i++)
                {
                    Response.Write("<br/>headers: " + i.ToString() + ": " + res.Headers[i]);
                }
            }
        }
    }

但是在这一行:

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();

我收到此错误:

enter image description here

为什么我会收到此错误以及如何解决此问题?

提前谢谢。

2 个答案:

答案 0 :(得分:1)

尝试在HTTP请求中设置Accept标头。像这样:

Accept: text/html,*/*;q=0.9

HTTP 406表示此标头是预期的但是丢失,或者它存在但没有指定与资源兼容的Content-Type(例如,在请求JPEG图像时请求Accept: text/html。)

另请参阅Accept-CharsetAccept-EncodingAccept-Language标题,这些标题也可以触发此状态代码。

答案 1 :(得分:1)

我的问题与format = XML&gt;有关。将它更改为format = json并将accept和content类型标题更改为appliocation / json,返回我试图获取的电子邮件

Get Yahoo Contact List in C# application