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
607 views
in Technique[技术] by (71.8m points)

json - The underlying connection was closed: An unexpected error occurred on a receive. when using Invoke-RestMethod from Azure powershell runbook

I'm trying to send Microsoft Azure Alerts to Google Chat room (Hangouts) from within Azure Powershell Runbook. From time to time the message is sent without any issues, but roughly in half of the tries it returns the error below:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive. 
At line:43 char:1 
+ Invoke-RestMethod -uri $uriHangouts -Method Post -body $body -Content ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException 
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Line 43 of my script looks like this

Invoke-RestMethod -uri $uriHangouts -Method Post -body $body -ContentType 'application/json'

Where $uriHangouts is the webhook address and the $body is message according to the schema provided here: https://developers.google.com/hangouts/chat/reference/message-formats/basic

As it is testing only, I'm sending every time exactly the same message. It is a basic one with some markup. If I forward it to Azure Logic App and resend it from there to Hangouts, it works every time. If I send it from my PC, it works every time as well. So far, I've tried without any success to use Invoke-WebRequest instead Invoke-RestMethod, and force TLS 1.2 (1.1) with the following line:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

I'm literally out of ideas where to go next.

Please, help! Thank you in advance!

question from:https://stackoverflow.com/questions/66050990/the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-receive

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

1 Reply

0 votes
by (71.8m points)

You do not need to use Invoke-WebRequest instead Invoke-RestMethod, you just need to run ?[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 before executing Invoke-RestMethod as this blog mentioned.

And if you want use Invoke-WebRequest instead Invoke-RestMethod, you can have a try with add -UseBasicParsing after Invoke-WebRequest command.


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

...