Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Http ornegi

Üzgünüm....yine Ingilizce... Place 4 TMemos, a TEdit, a TOpenDialog, a TNMHTTP, and 7 TButtons on the form. Component Descriptions Memo1: Header Display Memo2: Body Display Memo3: Status Display Memo4: Cookie Display Edit1: URL input Button1: HTTP Get Button2: HTTP Head Button3: HTTP Options Button4: HTTP Trace Button5: HTTP Put Button6: HTTP Post Button7: HTTP Delete Insert the following code into Button1's OnClick event: procedure TForm1.Button1Click(Sender: TObject); begin NMHTTP1.Get(Edit1.Text); end; When Button1 is clicked, the Get method is used to retrieved the document at the address specified by Edit1. Both the document body and the document header are retrieved. Insert the following code into Button2's OnClick event: procedure TForm1.Button2Click(Sender: TObject); begin NMHTTP1.Head(Edit1.Text); end; When Button2 is clicked, the Head method is used to retrieve the header of the document at the address specified by Edit1. Insert the following code into Button3's OnClick event: procedure TForm1.Button3Click(Sender: TObject); begin NMHTTP1.Options(Edit1.Text); end; When Button3 is clicked, the HTTP options for the document at the address in Edit1 are retrieved using the Options method. Note: Not all servers support the Options method. Insert the following code into Button4's OnClick event: procedure TForm1.Button4Click(Sender: TObject); var S: String; begin if InputQuery('Trace Data Required', 'Input data to send as trace', S) then NMHTTP1.Trace(Edit1.Text, S); end; When Button4 is clicked, the InputQuery function is used to get data from the user to use as trace data. If the user inputs data and clicks Ok, the Trace method sends the data to the server as trace data. Insert the following code into Button5's OnClick event: procedure TForm1.Button5Click(Sender: TObject); begin if OpenDialog1.Execute then begin NMHTTP1.OutputFileMode := TRUE; NMHTTP1.Put(Edit1.Text, OpenDialog1.FileName); NMHTTP1.OutputFileMode := FALSE; end; end; When Button5 is clicked, OpenDialog1 prompts for a file. If a file is selected, the OutputFileMode property is set to TRUE, so that the data to be put will be read from the file specified. The Put method is used to store the file at the address specified by Edit1. When the file is put, the OutputFileMode property is returned to FALSE. Insert the following code into Button6's OnClick event: procedure TForm1.Button6Click(Sender: TObject); var S: String; begin if InputQuery('Post Data Required', 'Input data to Post', S) then NMHTTP1.Post(Edit1.Text, S); end; When Button6 is clicked, the InputQuery function is used to retrieve the data to be posted. If the Ok button is clicked, the data that was input is posted using the Post method to the document specified by the address in Edit1. Insert the following code into Button7's OnClick event: procedure TForm1.Button7Click(Sender: TObject); begin NMHTTP1.Delete(Edit1.Text); end; When Button7 is clicked, the Delete method attempts an HTTP Delete of the document specified by the address in Edit1. Insert the following code into NMHTTP1's OnAuthenticationNeeded event: procedure TForm1.NMHTTP1AuthenticationNeeded(Sender: TObject); var AnID, APass: String; begin InputQuery('Authentication required', 'Enter a user ID', AnID); InputQuery('Authentication required', 'Enter a password', APass); NMHTTP1.HeaderInfo.UserId := AnID; NMHTTP1.HeaderInfo.Password := APass; ShowMessage('Authentication information in place, please retry the previous command'); end; If basic authentication is used to access the document specified by the address in Edit1, the OnAuthenticationNeeded event is called. In this example, the InputQuery function is used to retrieve a user ID and password. These values are then stored in the UserId and Password properties of the HeaderInfo property (See the THeaderInfo reference). A message is shown to the user asking them to attempt the HTTP transaction again once the password and user id are in place. Insert the following code into NMHTTP1's OnFailure event: procedure TForm1.NMHTTP1Failure(Cmd: CmdType); begin Memo1.Text := NMHTTP1.Header; Memo2.Text := NMHTTP1.Body; case Cmd of CmdGET: Memo3.Lines.Add('HTTP GET Failed'); CmdPOST: Memo3.Lines.Add('HTTP Post Failed'); CmdHEAD: Memo3.Lines.Add('HTTP HEAD Failed'); CmdOPTIONS: Memo3.Lines.Add('HTTP OPTIONS Failed'); CmdTrace: Memo3.Lines.Add('HTTP TRACE Failed'); CmdPut: Memo3.Lines.Add('HTTP PUT Failed'); CmdDelete: Memo3.Lines.Add('HTTP Delete Failed'); end; end; When an HTTP command fails, the OnFailure event is called. In this case, the header for the returned error is displayed in Memo1, and the body is displayed in Memo2. Memo3 is updated by checking which command failed using the Cmd parameter, and adding a specific fail message for each of the supported commands. Insert the following code into NMHTTP1's OnRedirect event: procedure TForm1.NMHTTP1Redirect(var Handled: Boolean); begin If MessageDlg('This site is redirecting you to another site. Allow redirect?', mtConfirmation, [mbYes, mbNo], 0) = mrNo then Handled := TRUE; end; If the document specified by the address in Edit1 redirects the client to another site for it's content, the OnRedirect event is called. Using the MessageDlg function, the user is asked to allow the redirect. If the user selects No, the Handled parameter is set to TRUE, which prevents the redirect (default action) from taking place. If the user selects Yes, the default action is taken by the component, and the redirected document is loaded. Insert the following code into NMHTTP1's OnSuccess event: procedure TForm1.NMHTTP1Success(Cmd: CmdType); begin if NMHTTP1.CookieIn <> '' then Memo4.Text := NMHTTP1.CookieIn; Case Cmd of CmdGET: begin Memo1.Text := NMHTTP1.Header; Memo2.Text := NMHTTP1.Body; Memo3.Lines.Add('HTTP GET Successful'); end; CmdPOST: begin Memo1.Text := NMHTTP1.Header; Memo2.Text := NMHTTP1.Body; Memo3.Lines.Add('HTTP POST Successful'); end; CmdHEAD: begin Memo1.Text := NMHTTP1.Header; Memo2.Text := NMHTTP1.Body; Memo3.Lines.Add('HTTP HEAD Successful'); end; CmdOPTIONS: begin Memo1.Text := NMHTTP1.Header; Memo2.Text := NMHTTP1.Body; Memo3.Lines.Add('HTTP OPTIONS Successful'); end; CmdTrace: begin Memo1.Text := NMHTTP1.Header; Memo2.Text := NMHTTP1.Body; Memo3.Lines.Add('HTTP TRACE Successful'); end; CmdPut: begin Memo1.Text := NMHTTP1.Header; Memo2.Text := NMHTTP1.Body; Memo3.Lines.Add('HTTP PUT Successful'); end; CmdDelete: begin Memo1.Text := NMHTTP1.Header; Memo2.Text := NMHTTP1.Body; Memo3.Lines.Add('HTTP DELETE Successful'); end; end; end; When an HTTP command succeeds, the OnSuccess event is called, signifying the success. In this example, if a cookie is returned from the remote host in the CookieIn property, it is displayed in Memo4. The header and body returned from the server in the Header and Body properties, respectively. The header is displayed in Memo1, and the body is displayed in Memo2. Memo3 acts as a status screen, displaying that the command specified by the Cmd parameter was successful.