简体   繁体   中英

Google Safe Browsing v4 API java

Here is the code to see which url is safe for browsing.I have used google api for it. The problem i am facing is I am not able to get SafeBrowsing class object to hit the given url. So kindly see if anyone have solution.

 public static void main(final String[] args) {
    try {
        final String baseURL = "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=xxx";
        final URL url = new URL(baseURL);

        // Get a URLConnection object, to write to POST method
        final HttpURLConnection connect = (HttpURLConnection) url.openConnection();
        connect.setRequestMethod("POST");
        connect.setRequestProperty("Content-Type", "application/json");
        // Specify connection settings
        connect.setDoInput(true);
        connect.setDoOutput(true);
        final ClientInfo clientInfo = new Ggooggle().getClientInfo();
        final ThreatInfo threatInfo = new Ggooggle().getThreatInfo();
        final FindThreatMatchesRequest request = new FindThreatMatchesRequest();
        request.setClient(clientInfo);
        request.setThreatInfo(threatInfo);

Try this.

public final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
public static final JacksonFactory GOOGLE_JSON_FACTORY = JacksonFactory.getDefaultInstance();

Safebrowsing.Builder safebrowsingBuilder = new Safebrowsing.Builder(httpTransport, GOOGLE_JSON_FACTORY, null);
Safebrowsing safebrowsing = safebrowsingBuilder.build();
FindThreatMatchesResponse findThreatMatchesResponse = safebrowsing.threatMatches().find(findThreatMatchesRequest).setKey(GOOGLE_API_KEY).execute();

List<ThreatMatch> threatMatches = findThreatMatchesResponse.getMatches();

if (threatMatches != null && threatMatches.size() > 0) {
    for (ThreatMatch threatMatch : threatMatches) {
        threatUrls.add(threatMatch.getThreat().getUrl());
    }
}

The full sample codes: https://github.com/kalinchih/java_google_safebrowsing_v4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM