Jump to content

sending a http request


MOGSY

Recommended Posts

Hi 

How is possible in UniGui to send a https request and collect the response i.e. sending a request to Google and get the response.

 

https://maps.googleapis.com/maps/api/distancematrix/json?mode=driving&units=imperial&origins=52.779323,-1.211226&destinations=52.643617,-1.126523&key=AIzaSyC41PXsKVx3DmU0sbYy1coK8HUz9WhXFgM

Google response would like

{
   "destination_addresses" : [ "257 Belgrave Gate, Leicester LE1 3HU, UK" ],
   "origin_addresses" : [ "Cloudway Court, Belton Rd, Loughborough LE11 1LW, UK" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "12.4 mi",
                  "value" : 20029
               },
               "duration" : {
                  "text" : "27 mins",
                  "value" : 1615
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

 

Thank you.

 

Link to comment
Share on other sites

var h:THTTPClient; R:IHTTPResponse; s:TStringStream;
begin
   h:=THTTPClient.Create;
   s:=TStringStream.Create;
   try
     R:=h.Get('http://google.com/yourQuery', s);
     if R.StatusCode = 200 then
       s.DataString;  //<--- your JSON request
   finally
     FreeAndNil(h);
     FreeAndNil(s);
   end;
end;

 

  • Like 1
Link to comment
Share on other sites

uses System.Net.HttpClientComponent,

function getHTML(URL:string):string;
var
  aResponse: TStringStream;
  http: TNetHTTPClient;
begin
  result:='';
  try
    http:=TNetHTTPClient.Create;    
    aResponse := TStringStream.Create;
    try
      http.Get(url, aResponse);
      result:= aResponse.DataString;
    finally
      aResponse.Free;
      http.free;
    end;
  except
    on E:Exception do
      showMessage('Get htmlfile error: '+ E.Message);
  end;
end;

Using TNetHTTPClient makes it a lot easier to connect to different webservers that have strict requirements when it comes to SSL setup, than when using Indy.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...