System.Net.WebException:值不能为null。参数名称:src

时间:2019-05-07 15:32:03

标签: xamarin.forms push-notification httpwebrequest onesignal httpwebresponse

我正在使用onesignal进行推送通知。我向Web服务器请求时遇到一些错误,例如 HttpWebRequest-远程服务器返回了错误:(400)Bad Request System .Net.WebException:值不能为null。参数名称:src 。任何机构都知道这一点,请帮助我... 这是我的代码

using System.IO;
using System.Net;
using System.Text;

var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest;

request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";

request.Headers.Add("authorization", "Basic My_API_Key");

byte[] byteArray = Encoding.UTF8.GetBytes("{"
                                        + "\"app_id\": \"My_app_id\","
                                        + "\"contents\": {\"en\": \"English Message\"},"
                                        + "\"filters\": [{\"field\": \"tag\", \"key\": \"level\", \"relation\": \">\", \"value\": \"10\"}]}");

string responseContent = null;

try {
    using (var writer = request.GetRequestStream()) {
        writer.Write(byteArray, 0, byteArray.Length);
    }

    using (var response = request.GetResponse() as HttpWebResponse) {
        using (var reader = new StreamReader(response.GetResponseStream())) {
            responseContent = reader.ReadToEnd();
        }
    }
}
catch (WebException ex) {
    System.Diagnostics.Debug.WriteLine(ex.Message);
    System.Diagnostics.Debug.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
}

System.Diagnostics.Debug.WriteLine(responseContent);

0 个答案:

没有答案