微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?