我正在使用.net webservice通过JSON与外部API(Urban Airship)进行通信。在我的出站JSON中有一些不正确的东西,据说,随着我看到的400 Bad Request错误,他们正在向我返回一个JSON对象,它有错误记录,可以帮助我识别问题。
唯一的问题是,我无法弄清楚如何接受和阅读JSON错误消息......我得到的只是400 Bad Request。有没有办法可以修改我的代码来查看这个返回的对象?
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.ContentType = contentType;
req.Method = method;
req.ContentLength = jsonDataBytes.Length;
CredentialCache credentialCache = new CredentialCache();
string username = "x";
string password = y
credentialCache.Add(uri, "Basic", new NetworkCredential(username, password));
req.Credentials = credentialCache;
req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username + ":" + password)));
req.Accept = "application/vnd.urbanairship+json; version=3;";
dynamic stream = req.GetRequestStream();
stream.Write(jsonDataBytes, 0, jsonDataBytes.Length);
stream.Close();
dynamic response = req.GetResponse().GetResponseStream();
StreamReader reader = new StreamReader(response);
dynamic res = reader.ReadToEnd();
reader.Close();
response.Close();