在网络上找了一部分,但是都很不理想! 所以特放分求这样一个函数!调用这个函数后,可以直接得到网页的源代码!例如网页源代码:=函数(HTTP地址);主要是想利用这个函数来得到IP地址的!  

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject); 
    var 
      strTmp: string; 
    begin 
      strTmp := IdHTTP1.Get('http://stock.business.sohu.com/p/pf.php?pf=1'); end;
    少的,估计不好用……
    要么换POST方法试试
      

  2.   

    楼上的老兄,我说的是不用控件! 只用API写!
      

  3.   

    只知道API可以用shellexecute来打开网页,得到源代码恐怕没有相关的API功能吧……至少我不知道
    也有可能是我孤陋寡闻了,期待高手……
      

  4.   

    你可以用URLDownloadToFile来下载网页源码。
    如果是想取IP地址,直接用GetHostByName
      

  5.   

    function HttpRequest(
        const AURL  : string;
        const AAddress  : string;
        const ARedirect : Boolean;
        var   AStatus : Integer;
        var   AResultStream : TMemoryStream;
        const AHeader       : TStrings = nil
        ):Boolean;
    var
      hSession, hConnect, hRequest: hInternet;
      HostName  : string;
      Address   : string;
      FileName  : String;
      Buf: Pointer;
      dwBufLen, dwIndex: DWord;
      Data: Array[0..READ_LENGTH - 1] of Char;
      InternetFlag: DWord;
      BytesReaded: Integer;
      BytesToRead: cardinal;
      AcceptTypes : array[0..1] of PChar;
      Header  : string;  procedure ParseURL(URL: String; var HostName, FileName: String);
      var
        i: Integer;
      begin
        if Pos('http://', LowerCase(URL)) <> 0 then
          Delete(URL, 1, 7);    i := Pos('/', URL);
        HostName := Copy(URL, 1, i);
        FileName := Copy(URL, i, Length(URL) - i + 1);    if (Length(HostName) > 0) and (HostName[Length(HostName)] = '/') then
          SetLength(HostName, Length(HostName) - 1);
      end;  procedure AddHeader(const AStr  : string);
      begin
        HttpAddRequestHeaders(
            hRequest,
            PChar(AStr),
            Length(AStr),
            1
            );
      end;  procedure CloseHandles;
      begin
        InternetCloseHandle(hRequest);
        InternetCloseHandle(hConnect);
        InternetCloseHandle(hSession);
      end;begin
      ParseURL(AURL, HostName, FileName);
      Address := AAddress;
      if Address = '' then
        Address  :=  HostName;
      hSession := InternetOpen(
          nil,
          INTERNET_OPEN_TYPE_PRECONFIG,
          nil,
          nil,
          0
          );  hConnect := InternetConnect(
          hSession,
          PChar(Address),
          INTERNET_DEFAULT_HTTP_PORT,
          nil,
          nil,
          INTERNET_SERVICE_HTTP,
          0,
          0
          );  InternetFlag := INTERNET_FLAG_RELOAD;// or INTERNET_FLAG_NO_AUTO_REDIRECT;
      if not ARedirect then
        InternetFlag  :=  InternetFlag or INTERNET_FLAG_NO_AUTO_REDIRECT;  AcceptTypes[0]  :=  '*.*';
      AcceptTypes[1]  :=  nil;
      hRequest := HttpOpenRequest(
          hConnect,
          'GET',
          PChar(FileName),
          'HTTP/1.1',
          PChar(AURL),
          nil,//@AcceptTypes,
          InternetFlag,
          0
          );  AddHeader('host: ' + HostName);
      AddHeader('Accept: */*');
      AddHeader('Accept-Language: zh-cn');
      AddHeader('Accept-Encoding: gzip, deflate');
      AddHeader('User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)');
      AddHeader('Connection: Keep-Alive');  HttpSendRequest(
          hRequest,
          nil,//PChar(Header),
          0,//Length(Header),
          nil,
          0
          );  dwIndex := 0;
      dwBufLen := 1024;
      GetMem(Buf, dwBufLen);
      try
        Result := HttpQueryInfo(
            hRequest,
            HTTP_QUERY_STATUS_CODE,
            Buf,
            dwBufLen,
            dwIndex
            );
        AStatus :=  StrToIntDef(StrPas(PChar(Buf)), 0);
      finally
        FreeMem(Buf);
      end;  if Assigned(AResultStream) then
        AResultStream.Clear;
      if Result then
      begin
        BytesReaded := 0;
        if AResultStream = nil then
          AResultStream :=  TMemoryStream.Create;
        while True do
        begin
          if not InternetReadFile(
              hRequest,
              @Data,
              SizeOf(Data),
              BytesToRead
              ) then
            Break
          else if BytesToRead = 0 then
            Break
          else begin
            AResultStream.Write(Data[0], BytesToRead);
            inc(BytesReaded, BytesToRead);
          end;
        end;    Result := BytesReaded <> 0;
      end;  CloseHandles;
    end;
      

  6.   

    to :楼上Data: Array[0..READ_LENGTH - 1] of Char;  这句编译不过去,另外怎么调用啊?