We help IT Professionals succeed at work.

Sockets and http-requests

haeger
haeger asked
on
Hi.
I'm sure there is someone out there who can answer this:
I have this Delphi app that I want to run on on regular intervals that's supposed to connect to a webserver and a CGI on that server, giving that cgi a few parameters.

I've created a TClientSocket and tried to write to that, but it doesn't seem to work.
I've done it like this
-----------
procedure TForm1.Button1Click(Sender: TObject);
begin
     sock.Connect;
     lblstatus.caption := 'Connected';
     sock.Writeln('GET test.cgi?foo=one&bar=two');
end;
--------------

It seems to connect but I don't know how to send EOL to make it accept the call to the CGI.


Then I thought that there is probably some component in Delphi somewhere that could help me do this, and someone of you who read this probably knows exactly how to solve my problem.

Am I making any sense? I'd gladly provide more info if I have to clarify things.

TIA

//Haeger
Comment
Watch Question

Commented:
Hello Haeger,

dont use the Clientsocket. Just use the TNMHTTP-Component

( you can see this Example in your Delphi Online Help)


procedure TForm1.Button1Click(Sender: TObject);
var
  NMHTTP1: TNMHTTP;
begin
  NMHTTP1 := TNMHTTP1.Create(Self);
  NMHTTP1.InputFileMode := FALSE;
  NMHTTP1.OutputFileMode := FALSE;
  NMHTTP1.ReportLevel := Status_Basic;
  If CheckBox1.Checked then
  Begin
    NMHTTP1.Proxy := Edit11.Text;
    NMHTTP1.ProxyPort := StrToInt(Edit12.Text);
  End;
  With NMHTTP1.HeaderInfo do

  Begin
    Cookie := Edit5.Text;
    LocalMailAddress := Edit6.Text;
    LocalProgram := Edit7.Text;
    Referer := Edit8.Text;
    UserID := Edit9.Text;
    Password := Edit10.Text;
  End;
  NMHTTP1.Get(Edit1.Text);
  Memo1.Text := NMHTTP1.Body;
  Memo2.Text := NMHTTP1.Header;
  If NMHTTP1.CookieIn <> '' then
    ShowMessage('Cookie:'+#13#10+NMHTTP1.CookieIn);
  NMHTTP1.Free;
end;

Best Regards

Cesario

Commented:
Example:

 NMHTTP1.Get('http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20195548');
 showmessage ( nmhttp1.body);

Best regards

Cesario

Commented:
HI, haeger,
If the sock in your code is a TClientSocket then
I don't understand the "Connect" and "Writeln".
There are no such methods in this Component.
Would you mind to formulate your question more precisely?
Sincerely,
Nestorua.

Commented:
HI,
If sock in your code is a TClientSocket component
then I don't understand where you found such methods:
"Connect" and "Writeln".
There are no such methods in  TClientSocket.
So, describe more precisely what do you do.
Sincerely,
Nestorua.

Commented:
A line change is the same as #13#10 - so send your request like this:

    sock.Write('GET test.cgi?foo=one&bar=two'#13#10#13#10);

Commented:
Are you sure you can use GET ?
Some cgi's only accept POST.
Sounds very strange that you would need an EOL.

W

Commented:
umulig, review the HTTP protocol (V 0.9 in this case) for more information. For HTTP 1.0, this would be:

 sock.Write('GET test.cgi?foo=one&bar=two HTTP/1.0'#13#10#13#10);

Explore More ContentExplore courses, solutions, and other research materials related to this topic.