Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net powershell HttpListener http文件服务器_.net_File_Powershell_Http_Httplistener - Fatal编程技术网

.net powershell HttpListener http文件服务器

.net powershell HttpListener http文件服务器,.net,file,powershell,http,httplistener,.net,File,Powershell,Http,Httplistener,我正在尝试创建一个基本的Powershell脚本来通过http共享文件,我从internet上找到的一些示例中复制了代码。 我的代码大致如下: $listener = New-Object Net.HttpListener $listener.Prefixes.Add("http://+:8000/") $listener.Start() $response.Headers.Add("Content-Type","text/plain") $buffer = [Text.Encoding]::

我正在尝试创建一个基本的Powershell脚本来通过http共享文件,我从internet上找到的一些示例中复制了代码。 我的代码大致如下:

$listener = New-Object Net.HttpListener
$listener.Prefixes.Add("http://+:8000/")
$listener.Start()

$response.Headers.Add("Content-Type","text/plain")
$buffer = [Text.Encoding]::UTF8.GetBytes((GC (Join-Path $Pwd ($context.Request).RawUrl)))
$response.ContentLength64 = $buffer.Length
$response.OutputStream.Write($buffer, 0, $buffer.Length)
$response.Close()
$Context.Response.Close()
$listener.Stop()
我可以得到文件,但它没有下载到二进制文件中,例如,当我尝试下载Zip文件时,新的/下载的文件比原始文件大。我想这已经转换成ascii码了。 如何保留原始文件格式

我想我应该替换以下几行,但是什么是正确的行,允许我以二进制格式获取文件

$response.Headers.Add("Content-Type","text/plain")
$buffer = [Text.Encoding]::UTF8.GetBytes((GC (Join-Path $Pwd ($context.Request).RawUrl)))
我只需要一个基本的http文件服务器就可以获得一些“zip”、“iso”文件


提前感谢。

您不应该使用
GC
读取文件内容。GC(获取内容)用于读取带有指定编码选项的文本文件


使用
FileStream
或其他读取二进制文件内容的工具。

您不应该使用
GC
读取文件内容。GC(获取内容)用于读取带有指定编码选项的文本文件


使用
FileStream
或其他读取二进制文件内容的工具。

好的,回答这个问题太过分了,但提问者没有发布可用的代码,我不知道如何使用HttpListener创建PowerShell文件服务器。下面是使用HttpListener创建一个工作的Powershell文件服务器的示例代码。最终,原始问题的答案是
ContentType=“application/octet-stream”
。它将通用二进制数据传递给请求者

$VerbosePreference="Continue"
Clear-Host
$Continue=$true
$Listener=[System.Net.HttpListener]::new()
$Listener.Prefixes.Add("http://+:80/")
$Listener.Start()
while ($Continue) {
  $Context=$Listener.GetContext()
  Write-Verbose $Context.Request.RawUrl
  if ($Context.Request.QueryString.HasKeys()) {
    $Continue=$false
    $Context.Request.QueryString.Keys | ForEach-Object { "$_ = $($Context.Request.QueryString.GetValues("$_"))" }
  }
  else {
    if ($Context.Request.Url.Segments.Count -eq 1) {
      $Content=[System.Text.Encoding]::UTF8.GetBytes("<html><body>Hello World</body></html>")
      $Context.Response.ContentType="text/html"
      $Context.Response.ContentEncoding=[System.Text.Encoding]::UTF8
      $Context.Response.ContentLength64=$Content.Length
      $Context.Response.KeepAlive=$false
      $Context.Response.StatusCode=200
      $Context.Response.StatusDescription="OK"
      $Context.Response.OutputStream.Write($Content, 0, $Content.Length)
      $Context.Response.OutputStream.Close()
      $Context.Response.Close()
    }
    else {
      if ($Context.Request.Url.LocalPath.EndsWith(".zip")) { $Context.Response.ContentType="application/octet-stream" }
      else { $Context.Response.ContentType="text/plain" }
      if (Test-Path -Path ("C:"+$Context.Request.Url.LocalPath.Replace("/","\"))) {
        $Content=Get-Content -Encoding Byte -Path ("C:"+$Context.Request.Url.LocalPath.Replace("/","\"))
        $Context.Response.ContentEncoding=[System.Text.Encoding]::Default
        $Context.Response.ContentLength64=$Content.Length
        $Context.Response.KeepAlive=$false
        $Context.Response.StatusCode=200
        $Context.Response.StatusDescription="OK"
        $Context.Response.OutputStream.Write($Content, 0, $Content.Length)
        $Context.Response.OutputStream.Close()
        $Context.Response.Close()
      }
      else {
        Write-Warning "File not found."
        $Content=[System.Text.Encoding]::UTF8.GetBytes("<html><body>File not found.</body></html>")
        $Context.Response.ContentType="text/html"
        $Context.Response.ContentEncoding=[System.Text.Encoding]::UTF8
        $Context.Response.ContentLength64=$Content.Length
        $Context.Response.KeepAlive=$false
        $Context.Response.StatusCode=404
        $Context.Response.StatusDescription="File not found"
        $Context.Response.OutputStream.Write($Content, 0, $Content.Length)
        $Context.Response.OutputStream.Close()
        $Context.Response.Close()
      }
    }
  }
}
$Listener.Stop()

#http://localhost/
#http://localhost/temp/filename.zip
#http://localhost/temp/filename.txt.log
#http://localhost/?x=y  # This (or something like it) will make the listener quit.
$VerbosePreference=“继续”
清除主机
$Continue=$true
$Listener=[System.Net.HttpListener]::new()
$Listener.Prefixes.Add(“http://+:80/”)
$Listener.Start()
而(续){
$Context=$Listener.GetContext()
编写详细的$Context.Request.RawUrl
if($Context.Request.QueryString.HasKeys()){
$Continue=$false
$Context.Request.QueryString.Keys | ForEach对象{“$\=$($Context.Request.QueryString.GetValues($)”)
}
否则{
if($Context.Request.Url.Segments.Count-等式1){
$Content=[System.Text.Encoding]::UTF8.GetBytes(“Hello World”)
$Context.Response.ContentType=“text/html”
$Context.Response.ContentEncoding=[System.Text.Encoding]::UTF8
$Context.Response.ContentLength64=$Content.Length
$Context.Response.KeepAlive=$false
$Context.Response.StatusCode=200
$Context.Response.StatusDescription=“确定”
$Context.Response.OutputStream.Write($Content,0,$Content.Length)
$Context.Response.OutputStream.Close()
$Context.Response.Close()
}
否则{
if($Context.Request.Url.LocalPath.EndsWith(“.zip”){$Context.Response.ContentType=“application/octet stream”}
else{$Context.Response.ContentType=“text/plain”}
if(测试路径-Path(“C:+$Context.Request.Url.LocalPath.Replace(“/”,“\”)){
$Content=Get Content-Encoding Byte-Path(“C:+$Context.Request.Url.LocalPath.Replace(“/”,“\”))
$Context.Response.ContentEncoding=[System.Text.Encoding]::默认值
$Context.Response.ContentLength64=$Content.Length
$Context.Response.KeepAlive=$false
$Context.Response.StatusCode=200
$Context.Response.StatusDescription=“确定”
$Context.Response.OutputStream.Write($Content,0,$Content.Length)
$Context.Response.OutputStream.Close()
$Context.Response.Close()
}
否则{
写入警告“未找到文件”
$Content=[System.Text.Encoding]::UTF8.GetBytes(“未找到文件”)
$Context.Response.ContentType=“text/html”
$Context.Response.ContentEncoding=[System.Text.Encoding]::UTF8
$Context.Response.ContentLength64=$Content.Length
$Context.Response.KeepAlive=$false
$Context.Response.StatusCode=404
$Context.Response.StatusDescription=“未找到文件”
$Context.Response.OutputStream.Write($Content,0,$Content.Length)
$Context.Response.OutputStream.Close()
$Context.Response.Close()
}
}
}
}
$Listener.Stop()
#http://localhost/
#http://localhost/temp/filename.zip
#http://localhost/temp/filename.txt.log
#http://localhost/?x=y  #这(或类似的事情)将使侦听器退出。

好的,回答这个问题太过分了,但提问者没有发布可用的代码,我也不知道如何使用HttpListener创建PowerShell文件服务器。下面是使用HttpListener创建一个工作的Powershell文件服务器的示例代码。最终,原始问题的答案是
ContentType=“application/octet-stream”
。它将通用二进制数据传递给请求者

$VerbosePreference="Continue"
Clear-Host
$Continue=$true
$Listener=[System.Net.HttpListener]::new()
$Listener.Prefixes.Add("http://+:80/")
$Listener.Start()
while ($Continue) {
  $Context=$Listener.GetContext()
  Write-Verbose $Context.Request.RawUrl
  if ($Context.Request.QueryString.HasKeys()) {
    $Continue=$false
    $Context.Request.QueryString.Keys | ForEach-Object { "$_ = $($Context.Request.QueryString.GetValues("$_"))" }
  }
  else {
    if ($Context.Request.Url.Segments.Count -eq 1) {
      $Content=[System.Text.Encoding]::UTF8.GetBytes("<html><body>Hello World</body></html>")
      $Context.Response.ContentType="text/html"
      $Context.Response.ContentEncoding=[System.Text.Encoding]::UTF8
      $Context.Response.ContentLength64=$Content.Length
      $Context.Response.KeepAlive=$false
      $Context.Response.StatusCode=200
      $Context.Response.StatusDescription="OK"
      $Context.Response.OutputStream.Write($Content, 0, $Content.Length)
      $Context.Response.OutputStream.Close()
      $Context.Response.Close()
    }
    else {
      if ($Context.Request.Url.LocalPath.EndsWith(".zip")) { $Context.Response.ContentType="application/octet-stream" }
      else { $Context.Response.ContentType="text/plain" }
      if (Test-Path -Path ("C:"+$Context.Request.Url.LocalPath.Replace("/","\"))) {
        $Content=Get-Content -Encoding Byte -Path ("C:"+$Context.Request.Url.LocalPath.Replace("/","\"))
        $Context.Response.ContentEncoding=[System.Text.Encoding]::Default
        $Context.Response.ContentLength64=$Content.Length
        $Context.Response.KeepAlive=$false
        $Context.Response.StatusCode=200
        $Context.Response.StatusDescription="OK"
        $Context.Response.OutputStream.Write($Content, 0, $Content.Length)
        $Context.Response.OutputStream.Close()
        $Context.Response.Close()
      }
      else {
        Write-Warning "File not found."
        $Content=[System.Text.Encoding]::UTF8.GetBytes("<html><body>File not found.</body></html>")
        $Context.Response.ContentType="text/html"
        $Context.Response.ContentEncoding=[System.Text.Encoding]::UTF8
        $Context.Response.ContentLength64=$Content.Length
        $Context.Response.KeepAlive=$false
        $Context.Response.StatusCode=404
        $Context.Response.StatusDescription="File not found"
        $Context.Response.OutputStream.Write($Content, 0, $Content.Length)
        $Context.Response.OutputStream.Close()
        $Context.Response.Close()
      }
    }
  }
}
$Listener.Stop()

#http://localhost/
#http://localhost/temp/filename.zip
#http://localhost/temp/filename.txt.log
#http://localhost/?x=y  # This (or something like it) will make the listener quit.
$VerbosePreference=“继续”
清除主机
$Continue=$true
$Listener=[System.Net.HttpListener]::new()
$Listener.Prefixes.Add(“http://+:80/”)
$Listener.Start()
而(续){
$Context=$Listener.GetContext()
编写详细的$Context.Request.RawUrl
if($Context.Request.QueryString.HasKeys()){
$Continue=$false
$Context.Request.QueryString.Keys | ForEach对象{“$\=$($Context.Request.QueryString.GetValues($)”)
}
否则{
if($Context.Request.Url.Segments.Count-等式1){
$Content=[System.Text.Encoding]::UTF8.GetBytes(“Hello World”)
$Context.Response.ContentType=“text/html”
$Context.Response.ContentEncoding=[System.Text.Encoding]::UTF8
$Context.Response.ContentLength64=$Content.Length
$Context.Response.KeepAlive=$false
$Context.Response.StatusCode=200
$Context.Response.StatusDescription=“确定”
$Context.Response.OutputStream.Write($Content,0,$Content.Length)
$Context.Response.OutputStream.Close()
$Context.Response.Close()
}
否则{
if($Context.Request.Url.LocalPath.EndsWith(“.zip”){$Context.Response.ContentType=“application/octet stream”}
else{$Context.Response.ContentType=“text/plain”}
if(测试路径-Path(“C:+$Context.Request.Url.LocalPath.Replace(“/”,“\”)){
$Content=获取内容-编码字节-Pat