Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to call WebApi in Delphi

Shulou Source: shulou.com Published: 2022-06-02 09:40:24 09月30日 Update

How to call WebApi in Delphi? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

When Delphi calls WebApi, we use IXMLHttpRequest. Just add msxml to the unit reference.

It is very simple to call, defining HttpReq: IXMLHttpRequest

HttpReq: = CoXMLHTTPRequest.Create

HttpReq.open ('Post', url, False, EmptyParam, EmptyParam)

The first of the above parameters is the method used, GET writes' Get',Post, write 'Post', the second is the mailing address, and the latter is filled in by default.

HttpReq.setRequestHeader ('Accept',' application/json')

HttpReq.setRequestHeader ('Content-Type',' application/json')

Above is the format of the request header

HttpReq.send (parasjson); / / start search

In Send, if you use the GET method, fill in the EmptyParam directly. If you use the POST method, fill in your own JSON string here.

Resultjson: = HttpReq.responseText

Get the return information

The GET method must pay attention to one thing.

Delphi must add a timestamp'& timestamp='+inttostr (Windows.GetTickCount) when using IXMLHttpRequest's GET method. If there is no timestamp after that, it will only GET once and no longer request GET.

Self-encapsulated complete unit that calls WebApi

Unit DoWebApi

Interface

Uses

Variants, msxml, SysUtils, Windows

/ / WebApi

/ / GET

Function WebApiGet (url: string; var resultjson: string; parasstr:string=''): Integer

/ / Post

Function WebApiPost (url, parasjson: string; var resultjson: string): Integer

Implementation

/ / obtain Get

/ / delphi must be timestamped'& timestamp='+inttostr (Windows.GetTickCount) when using IXMLHttpRequest's GET method.

/ / if you don't add it, you will only GET once, and you will no longer GET.

/ / Added by Administrator 2016-09-23 16:17:08

Function WebApiGet (url: string; var resultjson: string;parasstr:string=''): Integer

Var

Transurl: string

HttpReq: IXMLHttpRequest

Begin

If parasstr =''then

Transurl: = url +'? timestamp=' + inttostr (Windows.GetTickCount)

Else

Transurl: = url +'?'+ parasstr +'& timestamp=' + inttostr (Windows.GetTickCount)

Result: =-1

Try

Try

HttpReq: = CoXMLHTTPRequest.Create

HttpReq.open ('get', transurl, False, EmptyParam, EmptyParam)

HttpReq.setRequestHeader ('Accept',' application/json')

HttpReq.setRequestHeader ('Content-Type',' application/json')

HttpReq.send (EmptyParam); / / start search

Resultjson: = HttpReq.responseText

Result: = 0

Except

Result: =-1

End

Finally

End

End

/ / Post

Function WebApiPost (url, parasjson: string; var resultjson: string): Integer

Var

HttpReq: IXMLHttpRequest

Begin

Result: =-1

Try

HttpReq: = CoXMLHTTPRequest.Create

HttpReq.open ('Post', url, False, EmptyParam, EmptyParam)

HttpReq.setRequestHeader ('Accept',' application/json')

HttpReq.setRequestHeader ('Content-Type',' application/json')

HttpReq.send (parasjson); / / start search

Resultjson: = HttpReq.responseText

Result: = 0

Except

Result: =-1

End

End

End.

This is the answer to the question about how to call WebApi in Delphi. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

Tags: Method time question search unit more help answer easy easy information content parameter address character string boy buddy that is format Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information Xiaomi MySQL Redmi