从Powershell上的HTTP请求获取响应链接

编程入门 行业动态 更新时间:2024-10-27 23:25:26
本文介绍了从Powershell上的HTTP请求获取响应链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下Powershell脚本对存储在我的硬盘中的图像运行Google搜索.

The following Powershell script runs a Google search of an image stored within my hard drive.

我如何获得进入结果页面所需的链接?是否可以导航到显示在其上的不同网页?

How can I get the link which is followed to get to the results page? Is it possible to navigate to the different webpages displayed on it?

我已经尝试过 $ request.Links |选择href 尝试获取链接列表,但这没有用.我还尝试将 Write-Output $ respStream 添加到代码中,但是随后它无法运行.

I've tried $request.Links | Select href to try and get a list of the links, but it didn't work. I've also tried to add Write-Output $respStream to the code, but then it doesn't run.

Set-ExecutionPolicy Bypass -scope Process -Force function Get-GoogleImageSearchUrl { param( [Parameter(Mandatory = $true)] [ValidateScript({ Test-Path $_ })] [string] $ImagePath ) # extract the image file name, without path $fileName = Split-Path $imagePath -Leaf # the request body has some boilerplate before the raw image bytes (part1) and some after (part2) # note that $filename is included in part1 $part1 = @" -----------------------------7dd2db3297c2202 Content-Disposition: form-data; name="encoded_image"; filename="$fileName" Content-Type: image/jpeg "@ $part2 = @" -----------------------------7dd2db3297c2202 Content-Disposition: form-data; name="image_content" -----------------------------7dd2db3297c2202-- "@ # grab the raw bytes composing the image file $imageBytes = [Io.File]::ReadAllBytes($imagePath) # the request body should sandwich the image bytes between the 2 boilerplate blocks $encoding = New-Object Text.ASCIIEncoding $data = $encoding.GetBytes($part1) + $imageBytes + $encoding.GetBytes($part2) # create the HTTP request, populate headers $request = [Net.HttpWebRequest] ([Net.HttpWebRequest]::Create('images.google/searchbyimage/upload')) $request.Method = "POST" $request.ContentType = 'multipart/form-data; boundary=---------------------------7dd2db3297c2202' # must match the delimiter in the body, above $request.ContentLength = $data.Length # don't automatically redirect to the results page, just take the response which points to it $request.AllowAutoredirect = $false # populate the request body $stream = $request.GetRequestStream() $stream.Write($data, 0, $data.Length) $stream.Close() # get response stream, which should contain a 302 redirect to the results page $respStream = $request.GetResponse().GetResponseStream() # pluck out the results page link that you would otherwise be redirected to (New-Object Io.StreamReader $respStream).ReadToEnd() -match 'HREF\="([^"]+)"' | Out-Null $matches[1] } $url = Get-GoogleImageSearchUrl "C:\Users\Path\filename.jpeg" Start-Process $url

推荐答案

如@soc所述,您不需要流就可以拉出url,移动到的位置在响应标头中:

As mentioned by @soc, you shouldn't need the stream to pull the url, the moved to location is in the response header:

$request = [Net.HttpWebRequest] ([Net.HttpWebRequest]::Create('images.google/searchbyimage/upload')) $request.AllowAutoredirect = $false ... $response = $request.GetResponse() if ($response.StatusCode -eq 302) { $redirect_url = $response.Headers["Location"] write-host $redirect_url }

更多推荐

从Powershell上的HTTP请求获取响应链接

本文发布于:2023-11-25 13:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629970.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   Powershell   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!