WebRequest 不适用于 IIS,但适用于 Visual Studio 调试或邮递员

问题描述

.net core 3.1 应用程序在 IIS 8.5 win 2012 R2 下运行

我尝试通过该网站使用 WebRequest 推送通知,但出现超时。这很令人沮丧,因为当我在 Postman 或什至在 Visual Studio 的调试模式中编写相同的 WebRequest 时,它正在工作!

enter image description here

问题在于 应用程序 尝试将网络请求发布到 fcm.googleapis.com

  1. 用户填写表单,
  2. 用户 POST 到 Web 应用程序,
  3. 应用程序使用用户帖子中的数据创建另一个 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);
            }

enter image description here

全部在服务器中直接测试...不是外部 PC, 我什至在 win 2012 R2 中安装了 Visual Studio 以在那里进行调试,但是配音时没有出现问题..只有在将其发布到 IIS 8.5 后

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...