1

我有这个代码:

        WebRequest request = WebRequest.Create("https://getpocket.com/v3/oauth/request");
        request.Proxy = WebRequest.DefaultWebProxy;
        request.Credentials = System.Net.CredentialCache.DefaultCredentials; ;
        request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
        request.Method = "POST";
        string postData = "{\"consumer_key\":\"keyIsHere\",\"redirect_uri\":\"pickpocket:authorizationFinished\"}";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/json; charset=utf-8";
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse response = request.GetResponse();
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        Console.WriteLine(responseFromServer);

它返回code=12345456787blahblah而不是 JSON 响应:{"code":"12345456787blahblah"}我不知道为什么。我从MSDN获得了 POST 请求/响应代码,从Pocket API获得了正确的请求数据

4

1 回答 1

2

您需要添加X-Accept标题:

request.Headers["X-Accept"] = "application/json";


来自 API 文档:“X-Accept 标头指示您希望接收响应的格式,Pocket Authentication API 支持两种格式:application/x-www-form-urlencoded (DEFAULT)application/json"

于 2013-09-19T05:52:57.950 回答