因此,长话短说,我有一个使用MVC Web API作为后端的AJAX应用程序。但是,客户端从不同的域调用,并使用PHP代理文件来绕过跨域请求问题。
但是,使用PHP代理,Web API使用100 Continue
HTTP报头响应某些请求,并且任何获取该报头的请求都需要过长的时间才能完成(我们说的是2分钟左右),并且还可能返回无效响应。
此appears to be a known issue with cURL和解决方法通常被引用为在cURL请求中插入以下行以删除expect: 100标头
不幸的是,解决方案对我来说似乎难以捉摸:
$headers = getallheaders();
$headers_new = "";
foreach($headers as $title => $body) {
$headers_new[] = $title.": ".$body;
}
//$headers_new[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_new);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:') );
这段代码可以工作,但会删除所有其他头(这对我来说是不可行的,因为我使用HTTP basic auth头对API进行身份验证)。您可能还注意到,我尝试将Expect:
添加到现有的头文件中,但这对我也没有帮助。
如何维护现有的标头,同时又防止cURL期望100继续?
发布于 2015-09-02 22:38:19
谢谢你的剧本,哈克雷。由于HTTP PUT需要它,所以我对它进行了一些扩展,结果如下:
GET request ..........................................: Continue: No
GET request with empty header ........................: Continue: No
POST request with empty header .......................: Continue: Yes
POST request with expect continue explicitly set .....: Continue: Yes
POST request with expect (set to nothing) as well ....: Continue: Yes
POST request with expect continue from earlier removed: Continue: No
PUT request with empty header ........................: Continue: Yes
PUT request with expect continue explicitly set ......: Continue: Yes
PUT request with expect (set to nothing) as well .....: Continue: Yes
PUT request with expect continue from earlier removed : Continue: No
DELETE request with empty header .....................: Continue: Yes
DELETE request with expect continue explicitly set ...: Continue: Yes
DELETE request with expect (set to nothing) as well ..: Continue: Yes
DELETE request with expect continue from earlier removed : Continue: No
脚本如下:
<?php
$ch = curl_init('http://www.iana.org/domains/example/');
function curl_exec_continue($ch) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$continue = 0 === strpos($result, "HTTP/1.1 100 Continue\x0d\x0a\x0d\x0a");
echo "Continue: ", $continue ? 'Yes' : 'No', "\n";
return $result;
}
// --- GET
echo "GET request ..........................................: ", !curl_exec_continue($ch);
$header = array();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "GET request with empty header ........................: ", !curl_exec_continue($ch);
// --- POST
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('hello'));
echo "POST request with empty header .......................: ", !curl_exec_continue($ch);
$header[] = 'Expect: 100-continue';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "POST request with expect continue explicitly set .....: ", !curl_exec_continue($ch);
$header[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "POST request with expect (set to nothing) as well ....: ", !curl_exec_continue($ch);
unset($header[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "POST request with expect continue from earlier removed: ", !curl_exec_continue($ch);
// --- PUT
curl_setopt($ch, CURLOPT_PUT, TRUE);
$header = array();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "PUT request with empty header ........................: ", !curl_exec_continue($ch);
$header[] = 'Expect: 100-continue';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "PUT request with expect continue explicitly set ......: ", !curl_exec_continue($ch);
$header[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "PUT request with expect (set to nothing) as well .....: ", !curl_exec_continue($ch);
unset($header[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "PUT request with expect continue from earlier removed : ", !curl_exec_continue($ch);
// --- DELETE
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$header = array();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "DELETE request with empty header .....................: ", !curl_exec_continue($ch);
$header[] = 'Expect: 100-continue';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "DELETE request with expect continue explicitly set ...: ", !curl_exec_continue($ch);
$header[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "DELETE request with expect (set to nothing) as well ..: ", !curl_exec_continue($ch);
unset($header[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
echo "DELETE request with expect continue from earlier removed : ", !curl_exec_continue($ch);
?>
发布于 2020-11-11 14:27:47
要删除标题101,请继续使用此命令
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect:"));
https://stackoverflow.com/questions/14158675
复制相似问题