The URLCheck API checks for malicious URLs and provides you with security services which are easy-to-use, operations-free, and trustworthy, making implementing secure browsing services cheaper.
When a user visits a URL, this API checks whether the URL is a malicious one. If so, you can evaluate the risk and either warn the user of the risk or block the URL.
For details, please refer to Development Procedure.
To call the URLCheck API, perform the following steps:
1. Initialize the URLCheck API.
2. Request URL check.
3. Close the URL check session.
4. Process the result code.
Before using the URLCheck API, you must call the initUrlCheck() method to initialize the API. The sample code is as follows:
- SafetyDetectClient client = SafetyDetect.getClient(getActivity());
- client.initUrlCheck();
Specify the types of concerned threats as the input parameter of the URLCheck API. Constants in the UrlCheckThreat class include the threat types that can currently be addressed.
- package com.huawei.hms.safetydetect;
- public class UrlCheckThreat {
- // URLs of this type are marked as URLs of pages containing potentially malicious apps
- // (such as home page tampering URLs, Trojan-infected URLs, and malicious app download URLs).
- public static final int MALWARE = 1;
- // URLs of this type are marked as phishing and spoofing URLs.
- public static final int PHISHING = 3;
- }
Initiate a URL check request. The URL to be checked contains the protocol, host, and path but not the query parameter. The sample code is as follows:
- String url = "https://developer.huawei.com/consumer/en/";
- SafetyDetect.getClient(this).urlCheck(url, appId, UrlCheckThreat.MALWARE, UrlCheckThreat.PHISHING).addOnSuccessListener(this,new OnSuccessListener<UrlCheckResponse>() {
- @Override
- public void onSuccess (UrlCheckResponse urlResponse){
- if ( urlResponse.getUrlCheckResponse().isEmpty()) {
- // No threat exists.
- } else {
- // Threats exist.
- }
- }
- }).addOnFailureListener(this,new OnFailureListener() {
- @Override
- public void onFailure (@NonNull Exception e){
- // An error occurs during communication with the service.
- if (e instanceof ApiException) {
- // Huawei Mobile Service (APK) error code and corresponding error description.
- ApiException apiException = (ApiException) e;
- Log.d(TAG, "Error: " + CommonStatusCodes
- .getStatusCodeString(apiException.getStatusCode()));
- // Note: If the status code is SafetyDetectStatusCode.CHECK_WITHOUT_INIT,
- // you do not call the initUrlCheck() method or you have initiated
- // a URL check request before the call is completed.
- // If an internal error occurs during the initialization,
- // you need to call the initUrlCheck() method again to initialize the API.
- } else {
- // An unknown exception occurs.
- Log.d(TAG, "Error: " + e.getMessage());
- }
- }
- });
Call the getUrlCheckResponse() method of the URLCheckResponse object to obtain the URL check response. The method returns List<UrlCheckThreat> which contains a list of all detected URL threat types. If the list is empty, no threat has been detected. Otherwise, you can call the getUrlCheckResult () method of UrlCheckThreat to obtain the specific threat code. The sample code is as follows:
- final EditText testRes = getActivity().findViewById(R.id.fg_call_urlResult);
- List<UrlCheckThreat> list = urlCheckResponse.getUrlCheckResponse();
- if (list.isEmpty()) {
- testRes.setText("ok");
- } else {
- for (UrlCheckThreat threat : list) {
- int type = threat.getUrlCheckResult();
- }
- }
If your app does not need to call the URLCheck API anymore or will not need to for a while, you can call the shutdownUrlCheck() method to close the URL check session and release relevant resources.
- SafetyDetect.getClient(this).shutdownUrlCheck();
For details about the result codes, please refer to the API Reference.