尝试使用 curl post http 请求时出现 MSBuild 错误代码 9009

MSBuild error code 9009 when trying to post a http request using curl

我正在尝试 post 通过使用 Visual Studio 中的 "Publish" 按钮向 "new relic" 发送一个 http 请求。

在我的发布配置文件中,我选择 "web deploy" 然后完成我的设置。之后,我将进入我的“.pubxml”文件并在底部添加一个目标:

 <Target Name="AfterBuild">
 <Exec Command="curl -H &quot;x-api-key:mykey&quot; 
 -d &quot;deployment[application_id]=appId&quot; 
 -d &quot;deployment[description]=MSbuild deploy using curl&quot; 
 -d &quot;deployment[revision]=0&quot; 
 -d &quot;deployment[changelog]=Deployed using MSBuild&quot; 
 -d &quot;deployment[user]=User&quot; 
 -d http://api.newrelic.com/deployments.xml"/>    
 </Target>
</project>

在 visual studio 的错误列表中,我得到了这个:

Severity Code Description Project File Line Suppression State Error The command 
"curl 
-H "x-api-key:APPKEY" 
-d "deployment[application_id]=APPID" 
-d "deployment[description]=MSbuild deploy using curl" 
-d "deployment[revision]=1" 
-d "deployment[changelog]=Deployed using MSBuild" 
-d "deployment[user]=User" 
http://api.newrelic.com/deployments.xml" 
exited with code 9009

如果我 运行 curl.exe 中的 curl 命令有效(post 成功)但在 msbuild 期间无效。

正如您在上面看到的那样,所有接受 url 的东西都有引号,(也尝试过)

我试过像这样指定 curl 的路径

&quot;C:\WINDOWS\system32\curl.exe&quot; 

但这对天气不起作用。

我删除了 "ItemGroup",因为我不想包含网络配置(在网络配置上将构建操作设置为 "none")

我可以尝试什么?

已更新

<Target Name="AfterBuild"> and <Message Text="Test"/>..

这 运行ns 成功,但我在构建输出中看不到文本 "Test"。

 <Target Name="AfterBuild">
 <Exec WorkingDirectory="C:\Windows\System32\" Command="curl http://www.google.com"/>

此 returns 错误 9009。

证明它在正确的目录中:

在尝试重现您的问题时,我尝试了一些方法(运行ning curl、运行ning 64 位 curl、运行ning 在 PATH 中、运行 指定完整路径,运行 从 system32 目录中创建一个可执行文件),但我没有尝试过的一件事是将 curl 复制到 system32 目录:这样做几乎从来不是正确的解决方案问题,可以在互联网上找到的各种原因。是的,今天我们发现了它不好的另一个原因:]

MsBuild 的 Exec 任务通过创建一个包含 Command 到 运行 的临时文件,然后启动 cmd.exe /q /c <path/to/tempfile> 来工作。不过,该任务不只是开始 cmd.exe,而是通过在其前面添加 Environment.GetFolderPath(Environment.SpecialFolder.System) 来指定完整路径。从 64 位系统上的 32 位进程(即 msbuild)调用时 returns C:\windows\SysWOW64 或类似的。

因此 msbuild 从 C:\windows\SysWOW64\cmd.exe 启动 32 位 cmd。作为一个 32 位进程,file system redirection 被调用:如果你告诉一个 32 位进程在 c:\windows\system32 中查找,它不会那样做,而是在 c:\windows\SysWOW64 中查找。 curl.exe 不存在,因为你把它放在 system32 中。所以你得到一个代码 9009。

长话短说:做一个好 Windows 公民,不要将 curl.exe 放在 sSystem32 或 SysWOW64 中,然后在使用 Exec 时指定它的完整路径或添加目录它位于您的路径中。

curl 是 64 位的,如果你这样称呼它的话; %windir%\sysnative\curl