Location:Index > News >

Invoke HTTP API in NSIS installer to report installation behavior

Browsing History: 93Date: 2023-12-04

Today, I'll document the process of creating an installation package using nsNiuniuSkin+Nsis and how to use nsNiuniuSkin's interface for installation behavior reporting. I hope this will be helpful to everyone!

Background of the Issue

In the rapidly developing landscape of internet applications, data on user activity, new users, and installation or uninstallation counts are crucial for operations. How to implement installation behavior reporting during installation or uninstallation in the installation package and subsequently perform statistics on the backend is a significant challenge.

This has been a persistent issue for many of our clients. Although we have provided examples in the script, we still frequently receive inquiries from clients seeking further guidance. We hope that today's article will help everyone integrate more smoothly.

Implementation Solution

nsNiuniuSkin implements the ability to make HTTP interface requests in the plugin, supporting both GET and POST request methods. It also supports synchronous and asynchronous calling to accommodate different application scenarios.

With the ability to make interface calls, it becomes convenient to track specific behaviors during installation and report them. The server can then save the reported data in the database for subsequent statistical processing.

Practical Operation

Next, I will demonstrate the actual usage of nsNiuniuSkin+Nsis to report installation behavior:

1. Interface Description

nsNiuniuSkin::HttpInvoke "url" "customize_header" "post_data" async_flag

This interface is used to initiate HTTP requests in NSIS, mainly for installation behavior reporting and security verification.

Parameter Name Parameter Type Parameter Description Remarks
url String Full URL path to be requested  
customize_header String HTTP headers to be carried  
post_data String Data packet to be POSTed. If an empty string, it will be a direct GET request  
async_flag Int Whether it is an asynchronous request 0: Synchronous request, 1: Asynchronous request

0: Indicates a synchronous request. After the request, the result will indicate whether the request was successful (0 means success) along with the content.

1: Indicates an asynchronous request. It will start a thread within the plugin to make the request, and regardless of success or failure, there will be no result notification to the script (suitable for installation behavior reporting scenarios where results do not need to be handled).

customize_header generally takes one of the following two values:

Used in scenarios of posting key-value data:

Content-Type: application/x-www-form-urlencoded;charset=utf-8

Used when posting JSON data:

Content-Type: application/xml;charset=UTF-8

3. Call Examples

It's important to note that if using synchronous calls, they need to be placed in the BgWorker to avoid blocking the main thread. For asynchronous calls, consider the lead time of the call to avoid situations where the installation package exits, but the interface call is still incomplete.

GET request call can refer to the following code:

Synchronous call example (called in a function bound to BgWorker):

nsNiuniuSkin::HttpInvoke "http://www.ggniu.cn/test/test.html" "" "" 0
pop $R0
pop $R1
MessageBox MB_OK|MB_ICONINFORMATION "retval: $R0, retstr: $R1"

Asynchronous call example:

nsNiuniuSkin::HttpInvoke "http://www.ggniu.cn/test/test.html" "" "" 1

If you want to make a JSON POST call, you can refer to the following code:

Synchronous call example (called in a function bound to BgWorker):

StrCpy $R1 '{"mac":"xxxxx", "time":172929292992, "flag":1}'
nsNiuniuSkin::HttpInvoke "http://www.ggniu.cn/test/post.php" "Content-Type:application/xml;charset=UTF-8" "$R1" 0
pop $R0
pop $R1
MessageBox MB_OK|MB_ICONINFORMATION "retval: $R0, retstr: $R1"

Asynchronous call example:

StrCpy $R1 '{"mac":"xxxxx", "time":172929292992, "flag":1}'
nsNiuniuSkin::HttpInvoke "http://www.ggniu.cn/test/post.php" "Content-Type:application/xml;charset=UTF-8" "$R1" 1

If you want to make a key-value POST call, you can refer to the following code:

Synchronous call example (called in a function bound to BgWorker):

nsNiuniuSkin::HttpInvoke "http://www.ggniu.cn/test/post2.php" "Content-Type:application/xml;charset=UTF-8" "key1=1&key2=xxx" 0
pop $R0
pop $R1
MessageBox MB_OK|MB_ICONINFORMATION "retval: $R0, retstr: $R1"

Asynchronous call example:

nsNiuniuSkin::HttpInvoke "http://www.ggniu.cn/test/post2.php" "Content-Type:application/xml;charset=UTF-8" "key1=1&key2=xxx" 1

4. Choosing between Synchronous and Asynchronous Calls

Synchronous calls will block the current function call and return the server's response data to the script. This is suitable for scenarios where authentication and verification are needed. If only reporting installation behavior, both synchronous and asynchronous can be used. Consider the script placement carefully.

For behavior reporting during installation, we recommend placing it under the start_install: label in the OnBtnInstall function using asynchronous interfaces. Alternatively, place it in the CustomizeInstall function using a synchronous interface.

For uninstallation, it is recommended to place it in the un.OnCustomizeUnInstall function using an asynchronous interface.


 

Conclusion

In the installation package installation process, a beautiful UI often leaves a more profound impression on customers, reflecting the service provider's focus and dedication to user experience. We hope our efforts make installation package creation a bit easier and a bit more enjoyable!

May there be no difficult installation packages in the world!


Copyright © 2015 - 2023 SHENZHEN LEEQIA TECHNOLOGY CO., LTD All Rights Reserved