老子横刀向天笑 2015-01-10 10:46 采纳率: 66.7%
浏览 2815
已采纳

delphi HttpQueryInfo查询响应头信息,执行成功,但是返回的是空字符串

 procedure Get(url: string;res: TStream);
var
  hInt,hUrl:HINTERNET;
  buffer,hBuf:PChar;
  dwRead:cardinal;
  flag:Boolean;
  Len,index:Cardinal;
  stream:TStringStream;
  var value: DWORD;
begin
 GetMem(buffer, 65536);
 GetMem(hBuf, 65536);
 hInt := InternetOpen('Delphi',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
 dwRead:=0;
 hurl:=InternetOpenUrl(hInt,PChar(url),nil,0,INTERNET_FLAG_RELOAD,0);

 //查询响应头信息
 flag:=HttpQueryInfo(hurl,HTTP_QUERY_RAW_HEADERS_CRLF,hBuf,len,index);
 stream:=TStringStream.Create('');
 stream.Write(hBuf^,len);
 ShowMessage(stream.DataString);

 repeat
   InternetReadFile(hUrl,buffer,1000,dwRead);
   if dwRead<>0 then
     res.Write(buffer^, dwRead);
 until dwRead=0;

 InternetCloseHandle(hUrl);
 InternetCloseHandle(hInt);
 FreeMem(buffer);
end;

rt
delphi HttpQueryInfo查询响应头信息,执行成功,返回true,len也不为0,但是我从缓冲区读出来的字符串却是空的,不知道是我读取缓冲区的方式不对还是怎么的,有谁搞过,请多多指教!

  • 写回答

1条回答 默认 最新

  • pathletboy 2015-01-10 14:21
    关注

    procedure Get(const url: string; head, res: TStream);
    var
    hInt, hUrl: HINTERNET;
    buffer: PChar;
    dwRead: cardinal;
    flag: Boolean;
    len, index: Cardinal;
    begin
    GetMem(buffer, 65536);
    hInt := InternetOpen('Delphi', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
    hurl := InternetOpenUrl(hInt, PChar(url), nil, 0, INTERNET_FLAG_RELOAD, 0);
    len := 65536;
    index := 0;
    //查询响应头信息
    flag := HttpQueryInfo(hUrl, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, len, index);
    if flag then
    begin
    head.Write(buffer^, len);
    repeat
    if not InternetReadFile(hUrl, buffer, 1000, dwRead) then
    Break;
    if dwRead <> 0 then
    res.Write(buffer^, dwRead);
    until dwRead = 0;
    end;
    InternetCloseHandle(hUrl);
    InternetCloseHandle(hInt);
    FreeMem(buffer);
    end;

    procedure TForm1.btn1Click(Sender: TObject);
    var
    head, res: TStringStream;
    begin
    res := TStringStream.Create('');
    head := TStringStream.Create('');
    try
    Get('http://www.ip138.com', head, res);
    ShowMessage(head.DataString);
    ShowMessage(res.DataString);
    finally
    head.Free;
    res.Free;
    end;
    end;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗