我有一个正常工作的代码。
几天前,此代码开始出现此错误:
Error: c# the operation has timed out
StackTrace: at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at NotificationServices.SendMessageOneSignal(String userPlayerId, String message, String templateId)
我的代码:
public static void SendMessageOneSignal(string userPlayerId, string message, string templateId)
{
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", ConfigurationManager.AppSettings["OneSignalAuthorization"]);
var serializer = new JavaScriptSerializer();
var obj = new
{
app_id = ConfigurationManager.AppSettings["OneSignalAPPID"],
contents = new { en = message },
include_player_ids = new string[] { userPlayerId },
template_id = templateId
};
var param = serializer.Serialize(obj);
byte[] byteArray = Encoding.UTF8.GetBytes(param);
string responseContent = null;
try
{
using (var writer = request.GetRequestStream()) //Exception Error
{
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);
}
OneSignal文档尚可。我可以做些什么来工作?