简体   繁体   中英

Making PHP cURL request on Windows yields “400 Bad Request” from proxy

Morning all

Basically, I am unable to make successful cURL requests to internal and external servers from my Windows 7 development PC because of an issue involving a proxy server. I'm running cURL 7.21.2 thru PHP 5.3.6 on Apache 2.4.

Here's a most basic request that fails:

<?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}";

The following (complete) response is received.

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.

The log file generated by cURL contains the following.

* 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

Explicitly stating the proxy and user credentials, as in the following, makes no difference: the response is always the same.

<?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}";

I was surprised to see an absolute URL in the request line ('GET ...'), but I think that's fine when dealing with proxy servers - according to the HTTP spec.

I've tried all sorts of combinations of options - including sending a user-agent, following this and that, etc, etc - having been through Stack Overflow questions, and other sites, but all requests end in the same response.

The same problem occurs if I run the script on the command line, so it can't be an Apache issue, right?

If I make a request using cURL from a Linux box on the same network, I don't experience a problem.

It's the "Bad Request" thing that's puzzling me: what on earth is wrong with my request? Do you have any idea why I may be experiencing this problem? A Windows thing? A bug in the version of PHP/cURL I'm using?

Any help very gratefully received. Many thanks.

You might be looking at an issue between cURL (different versions between Windows and Linux) and your IronPort version. In IronPort documentation:

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]

Try removing the Proxy-Connection (or add a Connection ) header and see whether this solves the problem.

Also, you might want to compare the cURL logs between Windows and Linux hosts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM