我有一个巨大的请求,其内容在一个文件中。我收到了错误,我不知道为什么;我有这段代码:
Post Method invoked, but no request found!
但这会产生以下结果:
Request=GET%2C(0003128%2C0005588%2C0016308%2C0021216%2C0028262%2C0045614%2C0047245%2C0053673%2C0056650%2C0059585)%2C(stuff)%2C%2C%2C+%2CTitles%3DSHORT%2CDATEFORM%3DYMD
要清楚 - 文件包含这样的内容......(但实际上要大得多)
$creds = "UN" + ":" + "PW"
$strCusips = $cusips -join ","
$req1 = -join("GET,(", $strCusips, "),(stuff),,, ,Titles=SHORT,DATEFORM=YMD")
$request = "Request=" + [System.Net.WebUtility]::UrlEncode($req1)
$response = curl.exe POST --silent --user $creds --data $request http://url/cgi/stuff
是什么给出的?这是编码问题吗?它不应该是URL编码?或者是吗?
编辑:
为了更清楚一点,这很有效:
Get-Content temp.txt
(请注意我所做的只是将字符串传递给curl请求,而不是将其保存到文件然后传递文件。)
此外,这也有效,所以我知道我可以阅读该文件,其内容存在并且是正确的:
$strCusips1 = $cusips[0..9] -join ","
$req1 = -join("GET,(", $strCusips1, "),(CNTYRISKtxt),,,,Titles=SHORT,")
$request1 = "Request=" + [System.Net.WebUtility]::UrlEncode($req1)
$request1 > temp.txt
Get-Content temp.txt
curl.exe -v --user $creds --data-raw '@temp.txt' http://url/cgi/stuff
Request=GET%2C(0003128%2C0005588%2C0016308%2C0021216%2C0028262%2C0045614%2C0047245%2C0053673%2C0056650%2C0059585)%2C(CNTYRISKtxt)%2C%2C%2C%2
CTitles%3DSHORT%2C
curl.exe : % Total % Received % Xferd Average Speed Time Time Time Current
At line:26 char:1
+ curl.exe -v --user $creds --data-raw '@temp.txt' http:// ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ( % Total % ... Time Current:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
* Trying x.x.x.x...
* TCP_NODELAY set
* Connected to url (x.x.x.x) port 80 (#0)
* Server auth using Basic with user 'user'
> POST /cgi/stuff HTTP/1.1
> Host: http://url/cgi/stuff
> Authorization: Basic xxxxxxxxxxxxx
> User-Agent: curl/7.52.1
> Accept: */*
> Content-Length: 9
> Content-Type: application/x-www-form-urlencoded
>
} [9 bytes data]
* upload completely sent off: 9 out of 9 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 Script results follow
< Content-type: text/plain
<
{ [43 bytes data]
* Curl_http_done: called premature == 0
100 52 0 43 100 9 551 115 --:--:-- --:--:-- --:--:-- 551
Post Method invoked, but no request found!
* Closing connection 0
PS C:\Users\lhawk>
EDIT2:
我已经尝试了卷曲所有的每个--data- *标志,它们都会产生相同的结果。
EDIT3:
这就是我的运行结果:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.io.*" import="javax.xml.parsers.DocumentBuilderFactory,javax.xml.parsers.DocumentBuilder,org.w3c.dom.*" errorPage="" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form method="get" action="annotation.jsp">
<body>
<form method="get" action="annotation.jsp">
Michael
<div class="dropdown">
Owen
<div class="dropdown-content">
<br>
<input name = "gap" type="checkbox" value="definition">definition
<br>
<input name = "gap" type="checkbox" value="explanation">explanation
</div>
</div>
is an English former footballer who played as a striker for Liverpool, Real Madrid, Newcastle United, Manchester United and Stoke City, as well as for the England national team.
<input type="submit" name="submit" value="Submit">
</form>
</body>
</form>
</body>
</html>
答案 0 :(得分:0)
在上一个例子中,
curl.exe -v --user $creds --data-raw '@temp.txt' http://url/cgi/stuff
产生
> Content-Length: 9
@temp.txt
01234567890123456789
^ nine bytes
然后将命令切换回-d
以获取文件内容。
curl.exe -v -u $creds -d '@temp.txt' http://url/cgi/stuff
^^
由于您正在对响应进行编码,因此应该没问题。
$request = "Request=" + [System.Net.WebUtility]::UrlEncode($req1)
curl -v
进一步显示Post Method invoked, but no request found!
来自服务器。
< HTTP/1.0 200 Script results follow
< Content-type: text/plain
<
{ [43 bytes data]
* Curl_http_done: called premature == 0
100 52 0 43 100 9 551 115 --:--:-- --:--:-- --:--:-- 551
Post Method invoked, but no request found!
* Closing connection 0
无论语法错误如何,都是如此。
你提到两次字符串正常工作。
编辑:
为了更清楚一点,这很有效:
$creds = "UN" + ":" + "PW" $strCusips = $cusips -join "," $req1 = -join("GET,(", $strCusips, "),(stuff),,, ,Titles=SHORT,DATEFORM=YMD") $request = "Request=" + [System.Net.WebUtility]::UrlEncode($req1) $response = curl.exe POST --silent --user $creds --data $request http://url/cgi/stuff
(请注意我所做的只是将字符串传递给curl请求,而不是将其保存到文件然后传递文件。)
和
请求很好,因为我可以将它作为字符串传递,并获得所需的结果。
所以,我认为问题是由>
完成的编码。
"a happy string" > temp.txt
生成了一个用UCS-2 LE BOM编码的文本文件。文件以换行符结尾。
虽然,the newline does not matter。
-d, --data <data>
...
如果使用字母@开始数据,则其余部分应为文件名 从中读取数据,或者 - 如果您希望curl从中读取数据 标准输入。也可以指定多个文件。从文件中发布数据 因此命名为'foobar'将使用-d, - data @foobar。当--data 被告知从这样的文件中读取,回车和换行符 将被剥离。如果你不希望@字符有 特殊解释使用--data-raw代替。
以前,我写过a function to work around PowerShell's default encoding。所以,让我们使用该函数来查看文件的编码是否是问题。
function giveBinaryEqualFile ([string] $myInput, [string] $fileName)
{
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllText($fileName, $myInput, $Utf8NoBomEncoding)
}
# $request > "temp.txt" becomes
$myFile = "C:\full\path\temp.txt"
giveBinaryEqualFile $request $myFile
# $response = curl.exe -v --user $creds --data-raw '@temp.txt' http://url/cgi/stuff becomes
$response = curl.exe -v --user $creds --data '@temp.txt' http://url/cgi/stuff
# since you are referencing a file.
# --data-raw
# https://curl.haxx.se/docs/manpage.html
如果此操作开始有效,请将-v
切换为--silent
。