分享

Delphi tip#139: TWebBrowser and POST request

 quasiceo 2017-02-15

Today I want to describe how you can send some data to web-server from your remote script using TWebBrowser component.

var
  strData: string;
  PostData: OleVariant;
  Headers: OleVariant;
  i: Integer;
begin
  {1. you must create a string with parameter names and values
  Result string must be in the next format:
  Param1=Value1&Param2=Value2&Param3=Value3...}
  strData := 'Param1Name=' + HTTPEncode(Param1Value) + '&' +
    'Param2Name=' + HttpEncode(Param2Value) + ...;

  {2. you must convert a string into variant array of bytes and
   every character from string is a value in array}
  PostData := VarArrayCreate([0, Length(strData) - 1], varByte);

  { copy the ordinal value of the character into the PostData array}
  for i := 1 to Length(strData) do
    PostData[i-1] := Ord(strData[i]);

  {3. prepare headers which will be sent to remote web-server}
  Headers := 'Content-Type: application/x-www-form-urlencoded' + #10#13;

  {4. you must navigate to the URL with your script and send as parameters
  your array with POST-data and headers}
 
  yourWebBrowserComponent.Navigate('http://www./your_post_script.asp', EmptyParam, EmptyParam, PostData, Headers);
end;

Of course, the same task can be solved with any internet component which implement the http-client (not only TWebBrowser). But for every component the data for script will be posted by own rules (most components have pre-defined properties for this task).

For example, you can use my TSMIHTTPClient component from SMInternet suite. There you must set Action=haPost and fill the Headers property with own Post-data.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多