無法生成GET帶有自定義標頭的請求。
我在服務器上有一個非常簡單的 PHP 檔案:
<?php
if (empty($_GET['test']))
{
echo('0');
}
else
{
echo('1');
}
?>
我試過TIdHTTP:
procedure TForm1.Button1Click(Sender: TObject);
begin
IdHttp1.Request.CustomHeaders.AddValue('test', 'text');
Memo1.Text:= IdHttp1.Get('https://example.com');
end;
和THTTPClient:
procedure TForm1.Button1Click(Sender: TObject);
begin
NetHTTPClient1.CustHeaders.Add('test', 'text');
//NetHTTPClient1.CustomHeaders['test'] := 'text';
Memo1.Text:= NetHTTPClient1.Get('https://example.com', nil).ContentAsString;
end;
但該檔案僅呈現為0和/var/log/apache2/error.log包含:
PHP Notice: Undefined index: test in /var/www/html/index.php on line 3
好像CustomHeaders['test']是空的。為什么?
uj5u.com熱心網友回復:
您將標頭與引數混合在一起:
$_GET包含使用HTTP 方法時的所有引數GET。這始終是查詢地址本身:http://myserver.net/mysite.php?test=hell&other=world您將擁有引數test及其other各自的值。$_SERVER包含所有標題。標頭是與請求和回應一起發送的內容。Cookies 總是標題:Connection: keep-alive Accept-Ranges: bytes Set-Cookie: p=a; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
如果您想發送和訪問引數,只需將它們添加到您請求的地址:
IdHttp1.Get('https://example.com?test=text');
如果您想訪問 HTTP 標頭,請執行此操作而不是期望引數:
if (empty($_SERVER['test']))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/427684.html
