* * *

Author Topic: Using Winhttp  (Read 6405 times)

Timewarp

  • Full Member
  • ***
  • Posts: 144
Using Winhttp
« on: September 11, 2011, 04:47:32 pm »
Hello!

This code works fine (in Windows ofcourse)
Code: [Select]
uses comobj;

procedure TForm1.Button1Click(Sender: TObject);
var http: variant;
begin
 http:=createoleobject('WinHttp.WinHttpRequest.5.1');
 http.open('GET', 'http://lazarus.freepascal.org', false);
 http.send;
 showmessage(http.responsetext);
end;
This doesn't work. I don't understand, only difference is variable s, which contains the URL?
Code: [Select]
uses comobj;

procedure TForm1.Button1Click(Sender: TObject);
var http: variant;
s: string;
begin
 s:='http://lazarus.freepascal.org';
 http:=createoleobject('WinHttp.WinHttpRequest.5.1');
 http.open('GET', s, false);
 http.send;
 showmessage(http.responsetext);
end;
I have tried things like ansistring, but it just doesn't accept url. Both of these are okay in Delphi. Any suggestions?

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: Using Winhttp
« Reply #1 on: September 11, 2011, 05:24:49 pm »
Shouldn't s be declared as a variant i.s.o. a string?
1.0/2.6.0  XP SP3 & OS X 10.6.8

Timewarp

  • Full Member
  • ***
  • Posts: 144
Re: Using Winhttp
« Reply #2 on: September 11, 2011, 05:58:55 pm »
That works, thanks! I have always used string in delphi.

JD

  • Hero Member
  • *****
  • Posts: 1592
Re: Using Winhttp
« Reply #3 on: October 22, 2011, 08:06:19 pm »
Does anyone know how this can be implemented in Linux (without using Indy, Synapse etc)?

Thanks,

JD
Windows (10, 7) - Lazarus 1.8RC4/FPC 3.0.4 (also Delphi occasionally)
Linux Mint Cinnamon  - Lazarus 1.6.4/FPC 3.0.2
Indy 10.6 series; Zeos 7.2.1; SQLite, Firebird, PostgreSQL & MariaDB

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3537
Re: Using Winhttp
« Reply #4 on: October 22, 2011, 08:25:43 pm »
Does anyone know how this can be implemented in Linux (without using Indy, Synapse etc)?

Just read the source code of Synapse or lNet, they use UNIX sockets. But I think this is a bad idea. Synapse and LNet are excelent choices for getting a cross-platform sockets libraries and they are really light weight. Why would you want to limit yourself to a platform-dependent solution which is harder to develop? You will just end up reimplementing a stripped down lnet or synapse.

JD

  • Hero Member
  • *****
  • Posts: 1592
Re: Using Winhttp
« Reply #5 on: October 22, 2011, 08:39:39 pm »
Does anyone know how this can be implemented in Linux (without using Indy, Synapse etc)?

Just read the source code of Synapse or lNet, they use UNIX sockets. But I think this is a bad idea. Synapse and LNet are excelent choices for getting a cross-platform sockets libraries and they are really light weight. Why would you want to limit yourself to a platform-dependent solution which is harder to develop? You will just end up reimplementing a stripped down lnet or synapse.

Very good advice. You're right. Thanks a lot.

JD
Windows (10, 7) - Lazarus 1.8RC4/FPC 3.0.4 (also Delphi occasionally)
Linux Mint Cinnamon  - Lazarus 1.6.4/FPC 3.0.2
Indy 10.6 series; Zeos 7.2.1; SQLite, Firebird, PostgreSQL & MariaDB

DavidTh30

  • New member
  • *
  • Posts: 5
Re: Using Winhttp
« Reply #6 on: August 31, 2017, 05:58:25 pm »
The code depends on where you use.

Here you are.

-First code for getting the message from device use 'Microsoft.XMLHTTP'.
 So you need permission or login.
 please note:
 My testing device is "FL BT EPA" and "FL BT EPA MP" (communicate using AT language)

uses
  ...
  Variants, comobj, RegExpr; 

Function getText(strURL: variant): String;
var
  xstrResult:variant;
  WinHttpReq:variant;
  temp:variant;
begin
  WinHttpReq:=CreateOleObject('Microsoft.XMLHTTP');
  temp:=WinHttpReq.Open('GET',strURL,false);
  try
  WinHttpReq.Send();
  xstrResult:= WinHttpReq.responseText;
  except
    showmessage('Transfer not work');
    Exit;
  end;

  getText:=xstrResult;
end;       


procedure TForm1.Button1Click(Sender: TObject);
var
  re: TRegExpr;
  strAns:String;
begin
  re := TRegExpr.Create;
  re.Expression := '.*OK.*';
  strAns:=getText('http://10.0.0.100/form?at=at*amli=admin');
  if re.Exec(strAns) then showmessage('OK') else showmessage('not OK');
end;     



-Second code for getting the message from normal HTTP URL by object 'WinHttpRequest.5.1.'
 please note:
 Direct to WinHttp.WinHttpRequest.5.1 internet URL

uses
  ...
  Variants, comobj, RegExpr; 

Function getText2(strURL: variant): String;
var
  http :variant;
begin
  http:=createoleobject('WinHttp.WinHttpRequest.5.1');
  try
  http.open('GET', strURL, false);
  http.send;
  except
    showmessage('Transfer not work');
    Exit;
  end;
  getText2:=http.responsetext;
end;     

procedure TForm1.Button2Click(Sender: TObject);
var
  strAns:String; 
begin
  strAns:=getText2('http://lazarus.freepascal.org');
  Showmessage(strAns);
end;


/:^)

 8-) 8-) 8-) 8-) 8-) 8-) 8-) 8-)
« Last Edit: September 01, 2017, 03:31:02 am by DavidTh30 »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus