//Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("https://go.urbanairship.com/api/push/");
request.Credentials = new NetworkCredential("pvYMExk3QIO7p2YUs6BBkg", "rO3DsucETRadbbfxHkd6qw");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
//WRITE JSON DATA TO VARIABLE D
string postData = "{\"aps\": {\"badge\": 1, \"alert\": \"Hello from Urban Airship!\"}, \"device_tokens\": [\"6334c016fc643baa340eca25bc661d15055a07b475e9a6108f3f644b15dd05ac\"]}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
using (Stream dataStream = request.GetRequestStream())
{
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
}
// Get the response.
WebResponse response = request.GetResponse();
//Error "The remote server returned an error: (400) Bad Request"
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
using (Stream dataStream = response.GetResponseStream())
{
// Open the stream using a StreamReader for easy access.
using (var reader = new StreamReader(dataStream))
{
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
response.Close();
}
}
-
Please give a description of your question.– NixOct 6, 2010 at 13:58
-
I cleaned up the code a bit, to try and make a bit clearer.– Jason EvansOct 6, 2010 at 14:05
3 Answers
I have experienced a similar problem to what he is getting.
When the exception is thrown calling GetResponse(), it is a WebException. Cast it as such, then check out the response stream. Yes, the content length is -1, but ignore that.
catch (Exception ex)
{
//byte[] buffer = new byte[999999];
WebException wex = (WebException)ex;
var s = wex.Response.GetResponseStream();
string ss = "";
int lastNum = 0;
do
{
lastNum = s.ReadByte();
ss += (char)lastNum;
} while (lastNum != -1);
s.Close();
s = null;
ErrorHasOccurred(new Exception("An error has occurred sending the notification to Urban Airship. Please see the InnerException for details. Please note that, for sending messages, the master password is required (instead of the regular password). ERROR: " + ss, ex));
}
Then just breakpoint right where I have ErrorHasOccurred and read the contents of the ss
variable. It will tell you the actual error that Urban Airship returns.
-
1Thanks for the code snippet. It helped me find out that this was the cause for the 400 HTTP response: "Payload length must be less than or equal to 256 bytes"– StewieJun 12, 2012 at 12:21
-
Thta's exactly what this code snippet was used for - sending payloads to Urban Airship for Apple Notifications. And yes, payload total must be under 256 bytes - including message and any extra JSON formatted data you send with it. Don't forget to mark this as the answer if it helped you, and let me know if you have further questions. Jun 12, 2012 at 12:39
-
I wish Urban Airship could have documented this a few times and made it clear - I must admit that I didn't look around for it much. I always try to send a payload that is larger than 256 bytes and get bitten by this limitation.– StewieAug 8, 2012 at 7:40
What is your question? The server is saying your request is bad. Use Fiddler if you're not sure what you are actually sending to the server, then fix your request. Otherwise fix your server code.
Either way, this is "not a real question" fodder without some clarification.
-
Philip am still getting error. previously it work for me but now i am getting error.– UdayOct 6, 2010 at 14:11
-
It is a valid question...
First. Not use hard code for build the json string, use JavaScriptSerializer
var json = new JavaScriptSerializer().Serialize(yourObject);
Second. For single parameter, use ... BodyStyle = WebMessageBodyStyle.Bare, ... insted of BodyStyle = WebMessageBodyStyle.WrappedRequest,
(I spend a few hours with a similar problem)