Posting a web-form using TClientSocket. And how to use a web proxy-server (Views: 30)
Problem/Question/Abstract: Posting a web form using TClientSocket. This code snipset demonstrates posting directly and thru a web proxy-server. The result returned by the server is put into a string variable (FResult). Answer: { Copyright (c) 1999 by E.J.Molendijk This is a code snipset showing you how to post a form to a webserver. Look at the comments in the source for more details. Connect the following events to your ClientSocket: procedure T...Form.ClientSocket1Write; procedure T...Form.ClientSocket1Read; procedure T...Form.ClientSocket1Disconnect; procedure T...Form.ClientSocket1Error; It also shows how to route the transmission thru a web proxy-server. This is the format used to send to the webserver: Normal: 'POST ' + PostAddr + 'HTTP/1.0' + HTTP_Data + Content PROXY: 'POST http://' Webserver + PostAddr + 'HTTP/1.0' + HTTP_Data + Content } const WebServer = 'www.somehost.com'; WebPort = 80; PostAddr = '/cgi-bin/form'; { Proxy stuff is only needed if you use a proxy: } ProxyServer = 'proxy.somewhere.com'; ProxyPort = 3128; // Some data needed in the post heading HTTP_Data = 'Content-Type: application/x-www-form-urlencoded'#10 + 'User-Agent: Delphi/5.0 ()'#10 + { Yes! Promote Delphi 5! } 'Host: somewhere.com'#10 + 'Connection: Keep-Alive'#10; type T...Form = class(TForm) ... private { Private declarations } HTTP_POST: string; FContent: string; FResult: string; // This will hold the server responce public { Public declarations } end; { This functions does some url-encoding on St } { Eg. 'John Smith' => 'John+Smith' } function HTTPTran(St: string): string; var i: Integer; begin Result := ''; for i := 1 to length(St) do if St[i] in ['a'..'z', 'A'..'Z', '0', '1'..'9'] then Result := Result + St[i] else if St[i] = ' ' then Result := Result + '+' else Result := Result + '%' + IntToHex(Byte(St[i]), 2); end; procedure T...Form.ClientSocket1Write(Sender: TObject; Socket: TCustomWinSocket); begin // Post the data Socket.SendText(HTTP_POST + FContent); end; procedure T...Form.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket); begin // Incoming result data FResult := FResult + Socket.ReceiveText; end; procedure T...Form.ClientSocket1Disconnect(Sender: TObject; Socket: TCustomWinSocket); begin // YOU CAN PROCESS FResult HERE // end; procedure T...Form.ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer); begin ErrorCode := 0; // Ignore Errors end; { And here is the routine you can call to post your form data. } procedure T...Form.PostTheForm; begin // Clear result FResult := ''; // You can enter whatever fields you want // These are some examples: FContent := 'Name=' + HTTPTran('John Smith') + '&' + 'Address=' + HTTPTran('1 Waystreet') + '&' + 'Email=' + HTTPTran('jsmith@somewhere.com') + '&' + 'B1=Submit' + #10; // Calculate the contents length FContent := 'Content-Length: ' + IntToStr(Length(FContent)) + #10 + #10 + FContent; {-- Start proxy ---} { uncomment this code if you are using a proxy ClientSocket1.Host := ProxyServer; ClientSocket1.Port := ProxyPort; HTTP_POST := 'POST http://'+WebServer+PostAddr+' HTTP/1.0'#10; {--- End proxy ---} {--- Start normal connection --- } { remove this code if you are using a proxy } ClientSocket1.Host := WebServer; ClientSocket1.Port := WebPort; HTTP_POST := 'POST ' + PostAddr + ' HTTP/1.0'#10; {--- End normal ---} // Concat the header HTTP_Post := HTTP_Post + HTTP_Data; // Try to open connection ClientSocket1.Open; end; |