相关疑难解决方法(0)

如何使用WinInet api在Delphi中发送HTTP POST请求

我正在尝试使用WinInet函数从Delphi发出HTTP请求.

到目前为止,我有:

function request:string;
var
  hNet,hURL,hRequest: HINTERNET;
begin
  hNet := InternetOpen(PChar('User Agent'),INTERNET_OPEN_TYPE_PRECONFIG or INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if Assigned(hNet) then 
  begin
  try
    hURL := InternetConnect(hNet,PChar('http://example.com'),INTERNET_DEFAULT_HTTP_PORT,nil,nil,INTERNET_SERVICE_HTTP,0,DWORD(0));
    if(hURL<>nil) then
      hRequest := HttpOpenRequest(hURL, 'POST', PChar('param=value'),'HTTP/1.0',PChar(''), nil, INTERNET_FLAG_RELOAD or INTERNET_FLAG_PRAGMA_NOCACHE,0);
    if(hRequest<>nil) then
      HttpSendRequest(hRequest, nil, 0, nil, 0);
    InternetCloseHandle(hNet);
  except
    on E : Exception do
      ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
  end;
  end
end;
Run Code Online (Sandbox Code Playgroud)

但这没有做任何事情(我正在嗅探网络http流量,看它是否有效).我已成功使用InternetOpenURL但我还需要发送POST请求,该功能不会这样做.

有人能告诉我一个简单的例子吗?我想要的结果是将var响应页面作为字符串获取.

delphi post http wininet request

15
推荐指数
1
解决办法
3万
查看次数

如何使用WinInet在Delphi 2010中发送HTTP Post请求

我想使用WinInet在Delphi 2010中发送HTTP Post请求,但我的脚本不起作用; /

这是我的Delphi脚本:

uses WinInet;
procedure TForm1.Button1Click(Sender: TObject);
var
  hNet,hURL,hRequest: HINTERNET;
begin
  hNet := InternetOpen(PChar('User Agent'),INTERNET_OPEN_TYPE_PRECONFIG or INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if Assigned(hNet) then
  begin
  try
    hURL := InternetConnect(hNet,PChar('http://localhost/delphitest.php'),INTERNET_DEFAULT_HTTP_PORT,nil,nil,INTERNET_SERVICE_HTTP,0,DWORD(0));
    if(hURL<>nil) then
      hRequest := HttpOpenRequest(hURL, 'POST', PChar('test=test'),'HTTP/1.0',PChar(''), nil, INTERNET_FLAG_RELOAD or INTERNET_FLAG_PRAGMA_NOCACHE,0);
    if(hRequest<>nil) then
      HttpSendRequest(hRequest, nil, 0, nil, 0);
    InternetCloseHandle(hNet);
  except
      ShowMessage('error');
    end
  end;
end;
Run Code Online (Sandbox Code Playgroud)

和我的PHP脚本:

$data = $_POST['test'];
$file = "test.txt";
$fp = fopen($file, "a");
flock($fp, 2);
fwrite($fp, $data);
flock($fp, 3);
fclose($fp);
Run Code Online (Sandbox Code Playgroud)

delphi post http wininet

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

delphi ×2

http ×2

post ×2

wininet ×2

request ×1