-1

I want to load a url directly into a string without any data stream,what is the best way, internet open url works but it seems not clear.

I don't want to use any component for reading some short messages

Warren P
  • 65,725
  • 40
  • 181
  • 316
sam500
  • 1
  • 1
  • 1
  • what you mean with _load a url_? – jachguate Nov 03 '10 at 22:27
  • open a url like : 'http://test.com/postaccepter?=msg1=3444&msg2=test' – sam500 Nov 03 '10 at 22:29
  • Maybe have a look at http://stackoverflow.com/questions/2977720/how-to-send-a-http-post-request-in-delphi-2010-using-wininet/2977783#2977783 – Andreas Rejbrand Nov 03 '10 at 22:34
  • _open_ sounds like a browser operation and not a http one. You should define if you want to GET or POST over a particular URL. Sorry but it is not clear to me what you're trying to accomplish. By _without any data stream_ do you mean you want to discard the server response text or just you don't want to instantiate any TStream to retrieve it? – jachguate Nov 04 '10 at 06:50

4 Answers4

6

Delphi 6 and later ship with Indy, which has a TIdHTTP client component, eg:

uses
  ..., IdHTTP;

var
  Reply: String;
begin
  Reply := IdHTTP1.Get('http://test.com/postaccepter?=msg1=3444&msg2=test');
    ...
end;

Or:

uses
  ..., IdHTTP;

var
  Reply: TStream;
begin
  Reply := TMemoryStream.Create;
  try
    IdHTTP1.Get('http://test.com/postaccepter?=msg1=3444&msg2=test', Reply);
    Reply.Position := 0;
    ...
  finally
    Reply.Free;
  end;
end;

Depending on your needs.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
3

You can use Synapse, a very light weight library that has a simple function call to get just what your asking for:

uses
  classes, httpsend;

var
  Response : TStringlist;

begin
  if HttpGetText(URL,Response) then
    DoSomethingWithResponse(Response.Text);
end;

I would suggest getting the latest copy from SVN, which is more current and contains support for the latest versions of Delphi. There are also simple functions for posting form data, or retrieving binary resources. These are implemented as simple functions and are a great template if you want to do something extra, or that is not directly supported.

skamradt
  • 15,366
  • 2
  • 36
  • 53
2

You can use our SynCrtSock unit, which is even lighter than Synapse.

See http://synopse.info/fossil/finfo?name=SynCrtSock.pas

It is a self-contained unit (only one dependency with the WinSock unit), and it works from Delphi 6 up to Delphi XE.

You have these two functions available to get your data in one line of code:

/// retrieve the content of a web page, using the HTTP/1.1 protocol and GET method
function HttpGet(const server, port: AnsiString; const url: TSockData): TSockData;

/// send some data to a remote web server, using the HTTP/1.1 protocol and POST method
function HttpPost(const server, port: AnsiString; const url, Data, DataType: TSockData): boolean;

Or you can use textfile-based commands (like readln or writeln) to receive or save data.

TSockData is just a wrapper of RawByteString (under Delphi 2009/2010/XE) or AnsiString (up to Delphi 2007).

If you need also to write a server, you have dedicates classes at hand, resulting in fast processing and low resource consummation (it uses a Thread pool, and is implemented over I/O Completion Ports).

A.Bouchez
  • 246
  • 1
  • 2
0

If I'm already using XML in an application (and the MSXML2_TLB), I generally use IXmlHttpRequest to perform http operations. If you open and send the request, you can either use the response data as XML DOM using the ResponseXML, as text using ResponseText or as a data-stream using ResponseStream, see here for an example how to use this in Delphi: http://yoy.be/item.asp?i142

Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67
  • i don't Want to use any component or units the Andreas Rejbrand comment is good for me but is seems doesn't work – sam500 Nov 04 '10 at 14:33
  • msxml*.dll are almost surely present on all Windows machines, and if you want you could copy paste only the needed sections of MSXML2_TLB to avoid including the unit. – Stijn Sanders Nov 04 '10 at 19:29