使用 powershell 进行 http 请求

发布于 2024-12-08 19:57:38 字数 154 浏览 0 评论 0原文

我希望使用 powershell 向网页发出 http 请求,这可能吗?如果可以,我该如何实现这一点?

我可以向 https 页面发出请求吗?我可以使用 bat 文件发出 http 请求,但不能使用 https,希望我可以使用 powershell 发出 https 页面请求。

I am looking to make http requests to web pages with powershell, is this possible and if so, how may I achieve this?

Can I make requests to https pages? I am able to make http requests with a bat file but not https, was hoping I could https page requests with powershell.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夏日浅笑〃 2024-12-15 19:57:38

您可以使用 .NET 框架提供的常用 WebRequestHttpWebRequest 类。

$request = [System.Net.WebRequest]::Create('http://example.com')
# do something with $request

除了与 PowerShell 的语法差异之外,它与使用 C# 中的相同类和 API 没有什么不同。

PowerShell v3 还带来了 Invoke-WebRequest 和其他一些功能。

You can use the usual WebRequest and HttpWebRequest classes provided by the .NET framework.

$request = [System.Net.WebRequest]::Create('http://example.com')
# do something with $request

It's no different from using the same classes and APIs from C#, except for the syntactic differences to PowerShell.

PowerShell v3 also brings Invoke-WebRequest and a few others.

静若繁花 2024-12-15 19:57:38

试试这个:

(New-Object System.Net.WebClient).DownloadString("http://stackoverflow.com")

WebClient.DownloadString 方法(字符串)

或在 PowerShell 3.0 中,

(Invoke-WebRequest http://stackoverflow.com).content

Invoke-WebRequest

Try this:

(New-Object System.Net.WebClient).DownloadString("http://stackoverflow.com")

WebClient.DownloadString Method (String)

or in PowerShell 3.0,

(Invoke-WebRequest http://stackoverflow.com).content

Invoke-WebRequest

与风相奔跑 2024-12-15 19:57:38

根据您正在执行的操作,您还可以使用 System.Net.WebClient,它是 HttpWebRequest 的简化抽象,

$client = new-object system.net.webclient

请在此处查找差异:.NET 中的 WebClient 和 HTTPWebRequest 类之间有什么区别?

PS:使用 Powershell v3.0,您可以使用 Invoke-WebRequestInvoke-RestMethod cmdlet,它们可用于类似目的

Depending on what you are doing, you can also use System.Net.WebClient, which is a simplified abstraction of HttpWebRequest

$client = new-object system.net.webclient

Look here for difference: What difference is there between WebClient and HTTPWebRequest classes in .NET?

PS: With Powershell v3.0, you have Invoke-WebRequest and Invoke-RestMethod cmdlets which can be used for similar purposes

猫瑾少女 2024-12-15 19:57:38

如果所有其他方法都失败,请使用 http://curl.haxx.se 中的 Curl 。您可以设置所有内容,包括证书处理、POST 等。这并不微妙,但它可以工作并处理所有奇怪的情况;例如,您可以设置 --insecure 标志来忽略证书名称问题、过期或测试状态。

If all else fails, use Curl from http://curl.haxx.se . You can set everything, including certificate handling, POSTs, etc. Not subtle, but it works and handles all of the odder cases; e.g. you can set the --insecure flag to ignore certificate name issues, expiration, or test status.

长伴 2024-12-15 19:57:38

您可以使用 Invoke-WebRequest cmdlet 创建 HTTP、HTTPS、FTP 和 FILE 请求。这非常简单,并且提供了很多可供选择的选项。
示例:要向 google.com 发出简单的 http/https 请求,

Invoke-WebRequest -Uri "http://google.com"

请参阅MSDN

You can create HTTP, HTTPS, FTP and FILE requests using Invoke-WebRequest cmdlet. This is pretty easy and gives many options to play around.
Example: To make simple http/https requests to google.com

Invoke-WebRequest -Uri "http://google.com"

More references can be found MSDN

野生奥特曼 2024-12-15 19:57:38

此代码适用于 ASCII 和 ASCII。 powershell 中通过 https 的二进制文件:

# Add the necessary .NET assembly
Add-Type -AssemblyName System.Net.Http

# Create the HttpClient object
$client = New-Object -TypeName System.Net.Http.Httpclient

# Get the web content.
$task = $client.GetByteArrayAsync("https://stackoverflow.com/questions/7715695/http-requests-with-powershell")

# Wait for the async call to finish
$task.wait();

# Write to file
[io.file]::WriteAllBytes('test.html',$task.result)

在 Powershell 5.1.17134.1、Win 10 上测试

This code works with both ASCII & binary files over https in powershell:

# Add the necessary .NET assembly
Add-Type -AssemblyName System.Net.Http

# Create the HttpClient object
$client = New-Object -TypeName System.Net.Http.Httpclient

# Get the web content.
$task = $client.GetByteArrayAsync("https://stackoverflow.com/questions/7715695/http-requests-with-powershell")

# Wait for the async call to finish
$task.wait();

# Write to file
[io.file]::WriteAllBytes('test.html',$task.result)

Tested on Powershell 5.1.17134.1, Win 10

疯狂的代价 2024-12-15 19:57:38

尝试这个 PowerShell 模块:https://github.com/toolkitx/simple-request

安装安装模块-名称 SimpleRequest
然后你可以发送请求,如

$Data = @{
    "TokenUrl"     = 111
    "ClientSecret" = "222"
    "ClientId"     = "333"
    "AuthResource" = "444"
    "Username"     = "User1"
    "Password"     = "Password"
    "Id"           = 99
    "Price"        = 0.99
    "Value"        = "Content"
}

$Sample = '
POST https://httpbin.org/post?id={{Id}}

Content-Type: application/json
Authorization: Bearer {{QIBToken}}

{
    "id": {{Id}},
    "value": "{{Value}}"
}'

$Response = Invoke-SimpleRequest -Syntax $Sample -Context $Data

请参阅 GitHub 详细介绍

Try this PowerShell module: https://github.com/toolkitx/simple-request

Install by Install-Module -Name SimpleRequest
Then you can send requests like

$Data = @{
    "TokenUrl"     = 111
    "ClientSecret" = "222"
    "ClientId"     = "333"
    "AuthResource" = "444"
    "Username"     = "User1"
    "Password"     = "Password"
    "Id"           = 99
    "Price"        = 0.99
    "Value"        = "Content"
}

$Sample = '
POST https://httpbin.org/post?id={{Id}}

Content-Type: application/json
Authorization: Bearer {{QIBToken}}

{
    "id": {{Id}},
    "value": "{{Value}}"
}'

$Response = Invoke-SimpleRequest -Syntax $Sample -Context $Data

Please refer to GitHub for detail introductions

风蛊 2024-12-15 19:57:38

该方法下载内容:

# PowerShell 2 version
$WebRequest=New-Object System.Net.WebClient
$WebRequest.UseDefaultCredentials=$true
#$WebRequest.Credentials=(Get-Credential)
$Data=$WebRequest.DownloadData("http://<url>")
[System.IO.File]::WriteAllBytes("<full path of file>",$Data)

# PowerShell 5 version
Invoke-WebRequest -Uri "http://<url>" -OutFile "<full path of file>" -UseDefaultCredentials -ContentType 

This method downloads the content:

# PowerShell 2 version
$WebRequest=New-Object System.Net.WebClient
$WebRequest.UseDefaultCredentials=$true
#$WebRequest.Credentials=(Get-Credential)
$Data=$WebRequest.DownloadData("http://<url>")
[System.IO.File]::WriteAllBytes("<full path of file>",$Data)

# PowerShell 5 version
Invoke-WebRequest -Uri "http://<url>" -OutFile "<full path of file>" -UseDefaultCredentials -ContentType 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文