Web request property “URL” is empty

2019-07-29 08:00发布

问题:

I'm trying to figure out how to read the full URI from a request (Web.HTTPApp.TWebRequest). There is a URL property, however it returns empty, and I don't see any more properties which could give me this. How can I get the complete URI of the request? This is being used via Indy 10 TIdHTTPWebBrokerBridge.

For example...

http://www.mydomain.com:18500/SomePath/SomeFile.html?n=v&a=b

I tried tracking this URL property and found that in IdHTTPWebBrokerBridge.pas this is being left blank, apparently deliberately...

function TIdHTTPAppRequest.GetStringVariable(Index: Integer): AnsiString;
...
  case Index of
    ...
    //INDEX_URL             : Result := AnsiString(FRequestInfo.Document);
    INDEX_URL             : Result := AnsiString(''); // Root - consistent with ISAPI which return path to root 

回答1:

I don't know why INDEX_URL returns a blank string. Obviously, it was deliberate, though.

If you look at the IdHTTPWebBrokerBridge.pas code more carefully, you will see that INDEX_URL used to return FRequestInfo.Document, which is the same value that INDEX_PathInfo and INDEX_PathTranslated both return. So you could try using a combination of the TWebRequest.Host, TWebRequest.ServerPort, TWebRequest.Path..., and TWebRequest.Query properties to recreate the URL you are looking for (minus the protocol, which you will just have to hard-code).

Also, XE+ has a TWebRequest.RawPathInfo property which TIdHTTPAppRequest implements to return Indy's TIdHTTPRequestInfo.URI property value, so you should look at that as well.