powershell Invoke-WebRequest
不支持直接 -Headers
带入cookie,所以就写了个Invoke-Http
作为补充
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| function Invoke-Http {
Param ( [Parameter(Mandatory=$false)] [String] $cookie, [Parameter(Mandatory=$true)] [string] $url, [Parameter(Mandatory=$false)][ValidatePattern('POST|GET')] [String] $method='GET', [Parameter(Mandatory=$false)] [String] $post ) try { if($url.StartsWith('http://') -eq $false -and $url.StartsWith('https://') -eq $false ){ $url='http://'+$url }else{ $url=$url } if($url.StartsWith('https://')){ [Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } } [net.httpWebRequest] $req = [net.webRequest]::create($url) $req.UserAgent ='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36' $req.Headers.Add("Origin: $url") $req.ContentType='application/json' $req.Referer = $url $req.AllowAutoRedirect = $true if($cookie){ $req.Headers.add("Cookie: $cookie") } if($method -eq 'POST'){ $req.method = "POST" $reqst = $req.getRequestStream() $buffer = [text.encoding]::ascii.getbytes($post) $reqst.write($buffer, 0, $buffer.length) $reqst.flush() $reqst.close() }else{ $req.method = "GET" } [net.httpWebResponse] $res = $req.getResponse() $response = $res.getResponseStream() $sr = new-object IO.StreamReader($response) $result = $sr.ReadToEnd() return $result }catch { Write-Verbose "An exception was caught: $($_.Exception.Message)" $_.Exception.Response } }
|