问题描述
.net core 3.1 应用程序在 IIS 8.5 win 2012 R2 下运行
我尝试通过该网站使用 WebRequest 推送通知,但出现超时。这很令人沮丧,因为当我在 Postman 或什至在 Visual Studio 的调试模式中编写相同的 WebRequest 时,它正在工作!
问题在于 应用程序 尝试将网络请求发布到 fcm.googleapis.com
- 用户填写表单,
- 用户 POST 到 Web 应用程序,
- 应用程序使用用户帖子中的数据创建另一个 WebRequest 到 fcm.googleapis.com 并提交 POST 表单以将通知推送到 Android 设备
用户网络表单
[HttpPost]
public async Task<IActionResult> Create(Createviewmodel vm)
{
var request = new CreateRequest()
{
Author = String.IsNullOrEmpty(User.Identity.Name) ? "Guest" : User.Identity.Name,Description = vm.Description,};
var response = await _notificationService.Create(request);
vm.IsValid = response.IsSuccessfull;
vm.ShowBaseMessage = true;
vm.BaseMessage = response.Message;
return View("_CreateModalPartial",vm);
}
此数据传递给负责提交通知的服务
WebRequest 通知
try
{
var tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.Headers.Add(string.Format("Authorization: key={0}","apiKey"));
tRequest.Headers.Add(string.Format("Sender: id={0}","senderId"));
tRequest.ContentType = "application/json";
var payload = new
{
to = clientToken,data = new
{
author = author,description = description
}
};
string postbody = JsonConvert.SerializeObject(payload).ToString();
Byte[] byteArray = Encoding.UTF8.GetBytes(postbody);
tRequest.ContentLength = byteArray.Length;
using (Stream dataStream = await tRequest.GetRequestStreamAsync())
{
dataStream.Write(byteArray,byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
if (dataStreamResponse != null)
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
//result.Response = sResponseFromServer;
}
}
}
}
}
catch(Exception e)
{
_logger.LogWarning(e.Message);
}
全部在服务器中直接测试...不是外部 PC, 我什至在 win 2012 R2 中安装了 Visual Studio 以在那里进行调试,但是配音时没有出现问题..只有在将其发布到 IIS 8.5 后
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)