2017-10-06 38 views
1

這裏是代碼,看看哪些網址是安全瀏覽。我用它的谷歌API。 我面臨的問題是我無法獲得SafeBrowsing類對象來擊中給定的URL。 所以請看看是否有人有解決方案。Google安全瀏覽v4 API java

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); 

回答

1

試試這個。

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()); 
    } 
} 

完整的範例代碼:https://github.com/kalinchih/java_google_safebrowsing_v4

+0

這將有助於增加findThreatMatchesRequest'如何'是建立。 – alexod

+1

我將完整的示例代碼添加到https://github.com/kalinchih/java_google_safebrowsing_v4 – kalin