Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to call WebApi in Delphi

2025-03-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report