Getting the error "The remote server responded with the error: (400)" Bad request. "On line response WebResponse = request.GetResponse ();

//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();
    }
}
+3
source share
3 answers

I had a similar problem with what it gets.

When an exception calls GetResponse (), it is a WebException. Put it as such, and then check the response flow. Yes, the length of the content is -1, but ignore it.

        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 a breakpoint where I have ErrorHasOccurred and read the contents of the variable ss. He will tell you about the actual error that Urban Airship returns.

+10
source

? , . Fiddler, , , . .

, " ", - .

+2

This is the right question ...

Firstly. Do not use hard code to build json string, use JavaScriptSerializer

var json = new JavaScriptSerializer().Serialize(yourObject);

Secondly. For one parameter, use ... BodyStyle = WebMessageBodyStyle.Bare, ... implemented BodyStyle = WebMessageBodyStyle.WrappedRequest,

(I spend several hours with a similar problem)

0
source

Source: https://habr.com/ru/post/1768243/


All Articles