0

I am using a .net webservice to communicate via JSON with an external API (Urban Airship). There is something incorrect in my outbound JSON, and supposedly, along with the 400 Bad Request error I am seeing, they are returning a JSON object to me that has error logging that will help me identify the problem.

Only thing is, I can't figure out how to accept and read the JSON error message... all I get is the 400 Bad Request. Is there a way I can modify my code to see this returned object?

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();
Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
optionsix
  • 956
  • 1
  • 8
  • 27
  • 3
    What you have looks right, have you tried running up Fiddler and see if that catches any more? – Justin Harvey Feb 23 '15 at 14:25
  • its documentation says All API requests are HTTP requests, using the base url https://go.urbanairship.com/. For all requests with a body, the body must be in JSON format. When sending a request with a JSON body, set the Content-type header to application/json. Authentication – sakir Feb 23 '15 at 14:26
  • Similar question was already answered: http://stackoverflow.com/a/5409143/4585429 – rtf_leg Feb 23 '15 at 14:26
  • @rtf_leg - tried the WebException route already, the only thing it captures is : {"The remote server returned an error: (400) Bad Request."} – optionsix Feb 23 '15 at 14:38
  • @sakir - the above code works PERFECTLY for iOS... its Android thats causing a failure, and the JSON payload is almost identical. I'll try running it with Fiddler to see if I get anything else. – optionsix Feb 23 '15 at 14:40
  • This is sort of a non-answer, but Fiddler helped me identify the issue... it was an incorrect device token that was getting passed in my test device. Not sure if I should answer my own question with this, or just ask to close the question? – optionsix Feb 23 '15 at 14:59

0 Answers0