草庐IT

php - 在 Windows 上发出 PHP cURL 请求从代理中产生 "400 Bad Request"

coder 2024-06-15 原文

早上好

基本上,由于涉及代理服务器的问题,我无法从我的 Windows 7 开发 PC 向内部和外部服务器发出成功的 cURL 请求。我在 Apache 2.4 上通过 PHP 5.3.6 运行 cURL 7.21.2。

这是一个失败的最基本的请求:

<?php

$curl = curl_init('http://www.google.com');

$log_file = fopen(sys_get_temp_dir() . 'curl.log', 'w');

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_HEADER => TRUE,
    CURLOPT_STDERR => $log_file,
));

$response = curl_exec($curl);

@fclose($log_file);

print "<pre>{$response}";

收到以下(完整)响应。

HTTP/1.1 400 Bad Request
Date: Thu, 06 Sep 2012 17:12:58 GMT
Content-Length: 171
Content-Type: text/html
Server: IronPort httpd/1.1

Error response

Error code 400.

Message: Bad Request.

Reason: None.

cURL 生成的日志文件包含以下内容。

* About to connect() to proxy usushproxy01.unistudios.com port 7070 (#0)
*   Trying 216.178.96.20... * connected
* Connected to usushproxy01.unistudios.com (216.178.96.20) port 7070 (#0)
> GET http://www.google.com HTTP/1.1
Host: www.google.com
Accept: */*
Proxy-Connection: Keep-Alive

< HTTP/1.1 400 Bad Request
< Date: Thu, 06 Sep 2012 17:12:58 GMT
< Content-Length: 171
< Content-Type: text/html
< Server: IronPort httpd/1.1
< 
* Connection #0 to host usushproxy01.unistudios.com left intact

明确说明代理和用户凭据,如下所示,没有区别:响应始终相同。

<?php

$curl = curl_init('http://www.google.com');

$log_file = fopen(sys_get_temp_dir() . 'curl.log', 'w');

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_HEADER => TRUE,
    CURLOPT_STDERR => $log_file,
    CURLOPT_PROXY => 'http://usushproxy01.unistudios.com:7070',
    CURLOPT_PROXYUSERPWD => '<username>:<password>',
));

$response = curl_exec($curl);

@fclose($log_file);

print "<pre>{$response}";

我很惊讶在请求行中看到一个绝对 URL('GET ...'),但我认为在处理代理服务器时这很好 - 根据 HTTP 规范。

我已经尝试了各种选项组合 - 包括发送用户代理、跟踪这个那个等等 - 已经通过 Stack Overflow 问题和其他网站,但所有请求都以相同的响应结束。

如果我在命令行上运行脚本,也会出现同样的问题,所以这不可能是 Apache 的问题,对吧?

如果我从同一网络上的 Linux 机器使用 cURL 发出请求,我不会遇到问题。

令我困惑的是“错误请求”:我的请求到底有什么问题?你知道我为什么会遇到这个问题吗? Windows 的东西?我正在使用的 PHP/cURL 版本中存在错误?

任何帮助非常感激不尽。非常感谢。

最佳答案

您可能正在查看 cURL(Windows 和 Linux 之间的不同版本)和您的 IronPort 版本之间的问题。在 IronPort 文档中:

Fixed: Web Proxy uses the Proxy-Connection header instead of the Connection header, causing problems with some user agents

Previously, the Web Proxy used the Proxy-Connection header instead of the Connection header when communicating with user agents with explicit forward requests. Because of this, some user agents, such as Real Player, did not work as expected. This no longer occurs. Now, the Web Proxy replies to the client using the Connection header in addition to the Proxy-Connection header. [Defect ID: 46515]

尝试删除 Proxy-Connection(或添加 Connection) header ,看看是否能解决问题。

此外,您可能想要比较 Windows 和 Linux 主机之间的 cURL 日志。

关于php - 在 Windows 上发出 PHP cURL 请求从代理中产生 "400 Bad Request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12305558/

有关php - 在 Windows 上发出 PHP cURL 请求从代理中产生 "400 Bad Request"的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  8. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. ruby - 在 Windows 机器上使用 Ruby 进行开发是否会适得其反? - 2

    这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby​​-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub

随机推荐