Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
586 views
in Technique[技术] by (71.8m points)

c# - WebRequest Not working on IIS but works in Visual Studio debugging or postman

.net core 3.1 application running under IIS 8.5 win 2012 R2

I try to push notification using WebRequest through this website but I get timeout. it's flustrating because when i write the same WebRequest in Postman or even in debugging mode in Visual Studio it's working!

enter image description here

Problem is when the Application is trying to post webrequest to fcm.googleapis.com

  1. User fills up form,
  2. User POSTs to web application,
  3. Application creates another WebRequest with data from user post to fcm.googleapis.com and submits POST form in order to push notification to android devices

Web Form for users

[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);
}

This data is passed to service which is responsible for submiting notifications

WebRequest for notification

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, 0, 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

ALL TESTED DIRECTLY IN SERVER.. not external PC, I have even installed Visual Studio in win 2012 R2 to debug in there but the problem doesnt show up while dubbing.. only after publishing it to IIS 8.5

question from:https://stackoverflow.com/questions/65858111/webrequest-not-working-on-iis-but-works-in-visual-studio-debugging-or-postman

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...