Inno安装脚本中的HTTP POST请求

前端之家收集整理的这篇文章主要介绍了Inno安装脚本中的HTTP POST请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在Inno安装程序安装期间通过POST将一些从用户收集的信息提交到我们的服务器.

明显的解决方案是将安装程序提取到临时位置并使用参数启动的.exe文件.但是,我想知道 – 有没有更简单/更好的方法

解决方法

基于jsobo使用WinHttp库的建议,我带着这个非常简单的代码来实现这个窍门.假设您要在实际安装开始之前发送序列号进行验证.在代码部分,放:
procedure CurStepChanged(CurStep: TSetupStep);
var
  WinHttpReq: Variant;
begin
  if CurStep = ssInstall then
  begin
    if AutocheckRadioButton.Checked = True then
    begin
      WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
      WinHttpReq.Open('POST','<your_web_server>',false);
      WinHttpReq.SetRequestHeader('Content-Type','application/x-www-form-urlencoded');
      WinHttpReq.Send('<your_data>');
      // WinHttpReq.ResponseText will hold the server response
    end;
  end;
end;

Open方法将HTTP方法,URL以及是否执行异步请求作为参数,似乎我们需要添加SetRequestHeader,以便将Content-Type头设置为application / x-www-form-urlencoded.

WinHttpReq.Status将保存响应代码,以便检查服务器是否成功返回:

if WinHttpReq.Status <> 200 then
begin
  MsgBox('ERROR',mbError,MB_OK);
end
else
begin
  MsgBox('SUCCESS',mbInformation,MB_OK);
end;

http://msdn.microsoft.com/en-us/library/aa384106.aspx列出了WinHttpRequest对象的所有方法属性.

此外,为了避免运行时错误(如果主机无法访问,可能会发生),使用try / except代码来包围该代码是一个好主意.

猜你在找的Delphi相关文章