0

当我使用带有 xml 值的参数格式调用 Yahoo 用户配置文件 API 时,我遇到了一个奇怪的问题,yahoo 正在发送 JSONP(用括号覆盖的 JSON (<JSON>);)结果,但是当我通过 Google chrome 的邮递员插件 Yahoo api 发送此请求时发送 XML 结果。

API是:

http://social.yahooapis.com/v1/user/{guid}/profile _

文档:http: //developer.yahoo.com/social/rest_api_guide/extended-profile-resource.html

我的代码

 public string RestApiInvoke(string url, TRANSPORT_METHOD method, Dictionary<string, string> data, Dictionary<string, string> headers)
        {
            var client = new RestClient(url);
            RestRequest request;

            if (method == TRANSPORT_METHOD.POST)
            {
                request = new RestRequest(Method.POST);
            }
            else
            {
                request = new RestRequest(Method.GET);
            }

            if (data.IsNotNullOrEmpty())
            {
                foreach (var item in data)
                {
                    request.AddParameter(item.Key, item.Value);
                }
            }

            if (headers.IsNotNullOrEmpty())
            {
                foreach (var item in headers)
                {
                    request.AddHeader(item.Key, item.Value);
                }
            }

            IRestResponse response = client.Execute(request);

            return response.Content;
        }
4

1 回答 1

0

我得到了答案,只设置了Acceptheader text/xml,实际上 RestSharp 使用 Accept 之类的东西,application/json text/html text/xml所以 Yahoo 选择了第一个并发送 JSONP 结果。我已经使用以下代码手动设置了它。

headers.Add("Accept", "text/xml");
于 2013-06-20T12:34:37.613 回答