如何读取http请求标头内的值

时间:2017-05-03 10:14:01

标签: javascript

我想从http请求标头中提取特定值,下面是标题信息的示例

GET ***/agencychannel-uiapiservices/api/ken/AccountProfile/GetAccountProfileWithViewingPeriod*** HTTP/1.1
Cookie: __utma=132118163.703100490.1447412805.1456837339.1458655276.3; _em_vt=98cf395181af797bbccffc582cf957fe2438c7f254-0905822558184c34; A03RNB-PHRS2-80-PORTAL-PSJSESSIONID=a1Sfb2ZGJdR4VlBvyDvRDxSHOseo9kZa!-351035759; https%3a%2f%2fpeoplesoft.multichoice.co.za%2fpsp%2fhrprd%2femployee%2fhrms%2frefresh=list:%20%3Ftab%3Dhc_ux_manager_dashboard%7C%3Frp%3Dhc_ux_manager_dashboard%7C%3Ftab%3Dhc_talent_summary%7C%3Frp%3Dhc_talent_summary%7C%3Ftab%3Dremoteunifieddashboard%7C%3Frp%3Dremoteunifieddashboard
Host: ***apidtgateway.multichoice.co.za:9800***
Accept: */*
Content-Type: application/json
Accept-Language: en-ZA,en-GB;q=0.8,en-US;q=0.6,en;q=0.4
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36
Connection: keep-alive
komodo-sessiontoken: a2f960a2-f359-4ed9-a926-41630d37ae06
Accept-Encoding: gzip, deflate, sdch

当我收到请求时,我需要提取主机和路径,最终结果应该是这样apidtgateway.multichoice.co.za:9800/agencychannel-uiapiservices/api/ken/AccountProfile/GetAccountProfileWithViewingPeriod

1 个答案:

答案 0 :(得分:0)

示例

function TControllerAplicacao.EchoString(Value: string): string;
var
 objWebModule: TWebModule; //need Web.HTTPApp
 host :string;
 xxx :string;
begin
  objWebModule := GetDataSnapWebModule; //need Datasnap.DSHTTPWebBroker
  host := objWebModule.Request.host; 
  //see on Delphi IDE other possibilities, code complete will show to you)

  //key header that you know the name, is possible to be custom header:
  xxx := objWebModule.Request.GetFieldByName('Content-Type');

 Result := Value; //from original datasnap example EchoString

end;