delphi 7 http request into string

2019-06-14 16:04发布

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

4条回答
够拽才男人
2楼-- · 2019-06-14 16:15

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

查看更多
Ridiculous、
3楼-- · 2019-06-14 16:27

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.

查看更多
SAY GOODBYE
4楼-- · 2019-06-14 16:29

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).

查看更多
贼婆χ
5楼-- · 2019-06-14 16:31

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.

查看更多
登录 后发表回答