//大家看看这段代码是获取百度首页的网页源码,哪里需要改进啊,recv貌似一直在取……
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, WinSock2;type
  TForm1 = class(TForm)
    procedure FormClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormClick(Sender: TObject);
var
  wVersionRequested: WORD;
  wsaData: TWSAData;
  sktClient: TSocket;
  psa: PSockAddr;
  sHttpHead: array [0..1023] of Char;
  iReturn: Integer;
  buf: array [0..1023] of Char;
begin
  wVersionRequested := MakeWord(2, 0);
  if 0 <> WSAStartup(wVersionRequested, wsaData) then Exit;  sktClient := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if INVALID_SOCKET = sktClient then Exit;  New(psa);
  psa.sin_family := AF_INET;
  psa.sin_port := htons(80);
  psa.sin_addr.S_addr := inet_addr('202.108.22.5');
  if SOCKET_ERROR = connect(sktClient, psa, sizeof(psa^)) then Exit;//showmessage('aa');  sHttpHead :=
    'GET / HTTP/1.1' + #13 + #10 +
    //'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*' + #13 + #10 +
    //'Accept-Language: zh-cn' + #13 + #10 +
    //'Accept-Encoding: gzip, deflate' + #13 + #10 +
    //'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)' + #13 + #10 +
    'Host: www.baidu.com' + #13 + #10 +
    'Connection: Close' + #13 + #10;  if SOCKET_ERROR = send(sktClient, sHttpHead, strlen(sHttpHead), 0) then Exit;  repeat
    iReturn := recv(sktClient, buf, 1024, 0);
  until iReturn = SOCKET_ERROR;  ShowMessage(buf);  closesocket(sktClient);
  WSACleanUp();
  Dispose(psa);
end;end.

解决方案 »

  1.   

    //ASP返回数据 
    HTTP/1.1 200 OK 
    Cache-Control: private 
    Connection: close 
    Date: Thu, 06 Aug 2009 07:13:04 GMT 
    Content-Length: 10 
    Content-Type: text/html 
    Server: Microsoft-IIS/6.0 
    X-Powered-By: ASP.NET 
    Set-Cookie: ASPSESSIONIDCSQQQQSC=IACNBBEBGGJJHDPAMFDHILBP; path=/ 0123456789    //怎么取0123456789,总不能一下就返回一大堆,有什么函数或API吗 
    是返回这样的吗
      

  2.   

    http://topic.csdn.net/u/20080822/09/8be45d80-65ad-40cf-b2d0-58b048c18cf8.html
      

  3.   

    http://topic.csdn.net/u/20080822/09/8be45d80-65ad-40cf-b2d0-58b048c18cf8.html
      

  4.   

    楼主的方法我没有用过,原来确实也是用idhttp的,
    你在sktClient := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    创建一个socket,向'202.108.22.5'发tcp包,等待服务器返回,这个包是发给...
    返回的值 是什么
    如果一直读取的,那自然是没有返回错误的代码,否则返回的就是接收到的字节数了
      

  5.   

    socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    他能接受TCP包?
      

  6.   

    你的HTTP头错了,最后要用'Connection: Close' + #13 + #10 + #13 + #10;
      

  7.   

    用Socket直接去读网页内容,牛逼。顶一下,希望有高手指教。
      

  8.   

    同志們真不容易啊,終于解決了。有兩個問題:
    1、正如10L所說,http頭即sHttpHead最后要用'Connection: Close' + #13 + #10 + #13 + #10;要兩對回車換行。2、recv函數:
    Return ValuesIf no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
    所以改成:
      //var s: String;
      s := '';
      repeat
        ZeroMemory(@buf, 1024);
        iReturn := recv(sktClient, buf, 1024, 0);
        s := s + StrPas(buf) + #13 + #10;
      until (iReturn <= 0) or (iReturn = SOCKET_ERROR);