簡體   English   中英

獲取錯誤“遠程服務器返回錯誤:(400)錯誤請求。”在線WebResponse response = request.GetResponse();

[英]Getting error “ The remote server returned an error: (400) Bad Request.” on line WebResponse response = 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();
    }
}

我遇到了類似的問題。

當拋出異常調用GetResponse()時,它是一個WebException。 像這樣投射它,然后檢查響應流。 是的,內容長度為-1,但忽略它。

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

然后只是斷點,我有ErrorHasOccurred,並讀取ss變量的內容。 它會告訴你Urban Airship返回的實際錯誤。

你的問題是什么? 服務器說你的請求很糟糕。 如果您不確定實際發送到服務器的是什么,請使用Fiddler ,然后修復您的請求。 否則修復您的服務器代碼。

無論哪種方式,如果沒有一些澄清,這是“不是一個真正的問題”的飼料。

這是一個有效的問題......

第一。 不使用硬編碼來構建json字符串,使用JavaScriptSerializer

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

第二。 對於單個參數,請使用... BodyStyle = WebMessageBodyStyle.Bare,... inss of BodyStyle = WebMessageBodyStyle.WrappedRequest,

(我花了幾個小時遇到類似的問題)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM