我有这段代码:
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
答案 0 :(得分:2)
您需要添加X-Accept
标题:
request.Headers["X-Accept"] = "application/json";
从API文档:“X-Accept标头指示您希望接收响应的格式,Pocket Authentication API支持两种格式:application/x-www-form-urlencoded (DEFAULT)
和application/json"