C# 获取错误“;远程服务器返回错误:(400)请求错误。”;在线WebResponse=request.GetResponse();

C# 获取错误“;远程服务器返回错误:(400)请求错误。”;在线WebResponse=request.GetResponse();,c#,C#,你的问题是什么?服务器说您的请求不正确。如果您不确定实际发送到服务器的是什么,请使用,然后修复您的请求。否则,请修复服务器代码 不管怎样,这都不是一个没有澄清的“真正的问题”。我经历了一个与他所得到的类似的问题 调用GetResponse()引发异常时,它是一个WebException。将其转换为这样,然后检查响应流。是的,内容长度是-1,但是忽略它 //Create a request using a URL that can receive a post. WebRequest reques

你的问题是什么?服务器说您的请求不正确。如果您不确定实际发送到服务器的是什么,请使用,然后修复您的请求。否则,请修复服务器代码


不管怎样,这都不是一个没有澄清的“真正的问题”。我经历了一个与他所得到的类似的问题

调用GetResponse()引发异常时,它是一个WebException。将其转换为这样,然后检查响应流。是的,内容长度是-1,但是忽略它

//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();
    }
}

然后只需将断点指向发生错误的地方,并读取
ss
变量的内容它将告诉您城市飞艇返回的实际错误。

这是一个有效的问题

首先。不要使用硬代码构建json字符串,请使用JavaScriptSerializer

        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));
        }
第二。对于单个参数,使用 ... BodyStyle=WebMessageBodyStyle.Bare, ... 代替 BodyStyle=WebMessageBodyStyle.WrappedRequest


(我花了几个小时处理一个类似的问题)

请描述一下您的问题。我对代码进行了一些清理,试图让它更清楚一些。Philip仍然会出错。以前它对我有效,但现在我犯了错误。这是一个伟大的问题,它帮助了我!谢谢你的代码片段。它帮助我发现这就是400 HTTP响应的原因:“有效负载长度必须小于或等于256字节”Thta正是这个代码片段的用途——将有效负载发送到Urban Airship以获得苹果的通知。是的,负载总量必须在256字节以下——包括消息和随消息发送的任何额外JSON格式的数据。如果这对你有帮助的话,别忘了把它标记为答案,如果你还有其他问题,请告诉我。我希望城市飞艇能够记录这几次,并把它说清楚——我必须承认,我没有到处找它。我总是尝试发送一个大于256字节的有效负载,并受到这个限制。
var json = new JavaScriptSerializer().Serialize(yourObject);