This shows you the differences between two versions of the page.
— |
public:howto:httpgetpagethroughproxy [2007/11/30 14:30] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | Here's how to add your own headers to an HTTP request, download the page, display the headers and the page's body, and then use a regular expression to search for a pattern: | ||
+ | <code delphi> | ||
+ | //Uses TRegExpr library www.regexpstudio.com | ||
+ | uses | ||
+ | [...] httpsend, RegExpr; | ||
+ | |||
+ | procedure TForm1.Button1Click(Sender: TObject); | ||
+ | var | ||
+ | Header : TStringList; | ||
+ | Contents : TStringList; | ||
+ | URL : String; | ||
+ | Result : String; | ||
+ | Expression : string; | ||
+ | |||
+ | begin | ||
+ | Header := TStringList.Create; | ||
+ | Contents := TStringList.Create; | ||
+ | |||
+ | with THTTPSend.Create do begin | ||
+ | try | ||
+ | //Proxy launched on localhost:50000 | ||
+ | ProxyHost := 'localhost'; | ||
+ | ProxyPort := '50000'; | ||
+ | |||
+ | //Synapse already adds the following : How to remove and add one's own? | ||
+ | //User-Agent: Mozilla/4.0 (compatible; Synapse) | ||
+ | //Header.Add('User-Agent : Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'); | ||
+ | Header.Add('Accept-Language: fr-fr,en-us;q=0.7,en;q=0.3'); | ||
+ | Header.Add('Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'); | ||
+ | Headers.AddStrings(Header); | ||
+ | |||
+ | URL := 'www.cnn.com'; | ||
+ | |||
+ | //HttpGetText(URL, Contents) doesn't return headers | ||
+ | if not HTTPMethod('GET', URL) then begin | ||
+ | ShowMessage ('ERROR : ' + IntToStr(Resultcode)); | ||
+ | end else begin | ||
+ | ShowMessage(Headers.Text); | ||
+ | end; | ||
+ | |||
+ | //Let's save page's body (excluding headers) into a string and display it into a label | ||
+ | Contents.LoadFromStream(Document); | ||
+ | Result := Contents.Text; | ||
+ | Label1.Caption := Contents.Text; | ||
+ | |||
+ | //Let's check if we can find '<body>' in the page | ||
+ | with TRegExpr.Create do | ||
+ | try | ||
+ | ModifierI := True; | ||
+ | |||
+ | Expression := '<body>'; | ||
+ | if Exec (Result) then begin | ||
+ | label1.Caption := 'Found'; | ||
+ | end else begin | ||
+ | Label1.Caption := 'Not found' | ||
+ | end; | ||
+ | |||
+ | finally | ||
+ | Free; | ||
+ | end; | ||
+ | finally | ||
+ | Header.Free; | ||
+ | Contents.Free; | ||
+ | end; //try | ||
+ | end; //with THTTPSend.Create do begin | ||
+ | |||
+ | end; | ||
+ | </code> |