我有这个代码:
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 获得了正确的请求数据
您需要添加X-Accept
标头:
request.Headers["X-Accept"] = "application/json";
来自 API 文档:"X-Accept 标头指示您希望接收响应的格式,Pocket 身份验证 API 支持两种格式:application/x-www-form-urlencoded (DEFAULT)
和 application/json"