English 中文(简体)
PowerShell 上传 PowerShell 文件, 使用 PpeopGap 无法检索页面
原标题:PowerShell file upload with PhoneGap not retrieving page

我试图将档案文件(如果有关的话是Zip档案)上传到外部网站的现有 API, 虽然我可以让网络客户连接并获得上传从未起作用的网站的标记, 我却不断得到404个错误。 问题可能是在他们身上, 但我似乎是唯一一个在他们网站上张贴档案文件的人, 但是我不知道我是否正确使用上传文件, 或者有没有办法从命令中捕捉到回应, 我想知道是否只是为了改进我的 PowerShell 编码。

我竞选的情况如下:

# Variables I want to use
$appArchive = "\networkpathReleasesCodeReleasePackage.zip"
$localArchive = "c:	empCodeReleasePackage.zip"
[string]$appUrl = "https://site.com/api/v1/apps/(id)/"
[string]$tokenUrl = "https://site.com/token"
[string]$uploadUrl = "https://site.com/api/v1/apps/(id)/?auth_token=$mytoken"
# $appCred = Get-Credential
$username = "email@address"
$password = "password"
# Create the web client
"Creating the client.`n"
$pgup = new-object System.Net.WebClient
$pgup.Credentials = New-Object net.NetworkCredential($username,$password)
"Going to the token page"
$pgToken = $pgup.UploadString($tokenUrl,"")
$mytoken = $pgToken -replace  {|}|"|:|token ,  
"Now trying to upload the latest file with my token: $mytoken"
$pgup.UploadFile($uploadUrl,$appArchive)

我得到的牌子是 {"token",hkjuihgkjhiuhhlkhuhhkhuk}, 并且不知道是否有更好的方法可以处理所产生的牌子。 我做了替换来除掉符号字符串之外的所有牌子。 添加到 URL 应该是有效的, 但我只是得到了404个错误 。

所以当我运行 $pgup. UploadFile () 时, 我希望能够从网站上捕捉到响应。 上加文件 () 但是当我这样做时, 似乎没有任何东西会回来, 有没有特别的方法可以捕捉到呢? 我找不到一个清晰的例子, 当我做一些类似 $response = $pgup. UploadFile () 的东西时, 我没有任何答案。 任何有助于诊断网络客户错误的东西都会很好, 我想了解更多关于这个的信息, 因为它看起来非常有用 。

最佳回答

似乎我不能以PowerShell为司机访问网站, 出于某种原因我永远无法让上传工作, 但我可以读到网站上的页面, 并实际得到我账户的回复。 可能是他们的API不喜欢PowerShell的连接方式, 尽管我一直无法从PhoneGap上确认任何信息, 因为他们还没有对我的问题做出回应。

Edited: 6/14/12 The whole reason this was failing was due to the Apache implementation on their side not accepting the HTTP 1.1 request the way WebClient sends it. If I can get back to HTTP 1.0 then they say I should be able to complete this. Or I can wait for the OAUTH 2.0 implementation and just go from there.

问题回答

我不确定这是不是你们要找的, 但是这里有我用来上传一个拉链文件到服务器的代码。

$File = "yourfile.zip" #File to be uploaded
$ftp = "ftp://yourserver/directory #Location for the zip file to be uploaded


$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
$webclient.Credentials =  Get-Credential #Will prompt for user info. You can use your above way to gather credentials as well
$webclient.UploadFile($uri,$File)

正如你所看到的,它非常简短和简单,几乎与你的代码完全相同,但不处理符号。





相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签