Is it possible to send additional HTTP headers to web services via New-WebServiceProxy

Joe*_*oey 63 powershell web-services

A web service I need to interact with (often manually for testing) requires an extra HTTP header on certain requests. Quick manual testing works quite great with PowerShell's New-WebServiceProxy but so far I haven't found an option to add another HTTP header to the request.

Is there something for that?

Mad*_*kor 107

Invoke-WebRequest http://yourURLhere -Headers @{"accept"="application/json"}
Run Code Online (Sandbox Code Playgroud)

POSH 3.0中推出

  • 这在PowerShell中有效,但仅限v4及更高版本.在v3中,它会引发异常. (7认同)
  • 问题是关于在使用 cmdlet“New-WebServiceProxy”而不是“Invoke-WebRequest”时添加标头 (3认同)
  • 我收到一个错误,因为`Accept`是一个保留的标题名称.我最终使用了WebClient. (2认同)
  • 我在命令行上搜索 Invoke-WebRequest 标头很久之后才来到这里。多个标头可以用分号分隔: Invoke-WebRequest -Uri https://exampleURL -Headers @{'x-caller-name' = 'CallerName'; '内容类型' = 'application/json' ; } (2认同)

Wha*_*ool 21

您可以使用PowerShell的.NET Web客户端.

> $webClient = New-Object System.Net.WebClient
> $webClient.Headers.add('accept','application/json')
> $webClient.DownloadString('http://yourURLhere')
Run Code Online (Sandbox Code Playgroud)


Bri*_*ian 9

我很惊讶这没有出现:

$uri = 'http://...'

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Accept','Application/Json')
$headers.Add('X-My-Header','...')

$result = Invoke-WebRequest -Uri $uri -Headers $headers
Run Code Online (Sandbox Code Playgroud)

为完整起见,对headers属性的引用:

https://msdn.microsoft.com/en-us/library/s4ys34ea(v=vs.110).aspx