[英]Could a windows scheduled task connect to a rest endpoint?
I have a rest service written in ASP.NET Web API.我有一个用 ASP.NET Web API 编写的休息服务。
I want a scheduled task to connect to an endpoint like:我想要一个计划任务连接到一个端点,如:
www.example.com/jobs/job1
I want to be able to set the interval time to say every 12 hours.我希望能够将间隔时间设置为每 12 小时一次。
Is it possible to do this with a scheduled task?是否可以通过计划任务执行此操作?
I want to avoid having to create a windows service just to ping a rest endpoint.我想避免创建 Windows 服务只是为了 ping 一个休息端点。
You can easily accomplish this with PowerShell and System.Net.WebClient
.您可以使用 PowerShell 和
System.Net.WebClient
轻松完成此操作。
Create a simple MyScriptName.ps1
file with the following contents:使用以下内容创建一个简单的
MyScriptName.ps1
文件:
$web = New-Object System.Net.WebClient
$str = $web.DownloadString("http://www.example.com/jobs/job1")
$str # not strictly necessary but if you run this in PowerShell you will get the response body of your endpoint
Then create a new scheduled task and add a new action to Start a program
and use the following settings:然后创建一个新的计划任务并添加一个新操作来
Start a program
并使用以下设置:
Program/script: powershell
Add arguments: .\MyScriptName.ps1
Start in: C:\The\Directory\You\Saved\Your\Script\In
Starting with PowerShell 3.0 you can use the Invoke-RestMethod cmdlet.从 PowerShell 3.0 开始,您可以使用Invoke-RestMethod cmdlet。
Invoke-RestMethod -Uri "www.example.com/jobs/job1"
The advantage here is that it will deserialize it for you into an object if it's XML or JSON.这里的优点是,如果它是 XML 或 JSON,它会为您将其反序列化为一个对象。 For RSS or ATOM it will return the Item or Entry XML nodes.
对于 RSS 或 ATOM,它将返回 Item 或 Entry XML 节点。 For text it will display the text.
对于文本,它将显示文本。
You can read more here: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/invoke-restmethod您可以在此处阅读更多信息: https : //msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/invoke-restmethod
Create the scheduled task with the details below:使用以下详细信息创建计划任务:
Trigger: Daily, at your chosen time, repeat 12 hours for a duration of 1 day.
触发条件:每天,在您选择的时间,重复 12 小时,持续 1 天。
Actions:
行动:
Start a program: powershell
启动程序: powershell
Arguments: -NoProfile -NonInteractive -File ".\\YourScriptName.ps1"
参数: -NoProfile -NonInteractive -File ".\\YourScriptName.ps1"
Start in: C:\\your_scripts
开始于: C:\\your_scripts
Given that you have created an object you can format this data any way you choose but that's beyond the scope of this question.鉴于您已经创建了一个对象,您可以以您选择的任何方式格式化这些数据,但这超出了本问题的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.