加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 获取错误“远程服务器返回错误:(400)错误请求.”在线WebR

发布时间:2020-12-15 23:56:17 所属栏目:百科 来源:网络整理
导读://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 th
//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,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返回的实际错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读