简体   繁体   English

google Safebrowsing api v4 总是返回空响应

[英]google Safebrowsing api v4 always returns empty response

I am using safe browsing api v4 in my android code.I am using Asynctask and request safebrowsing api using httpurlconnection.The response is always empty.我在我的 android 代码中使用安全浏览 api v4。我使用 Asynctask 并使用 httpurlconnection 请求安全浏览 api。响应始终为空。

I have tested my connection using test url http://malware.testing.google.test/testing/malware/ also, then too it returns empty.我也使用测试 url http://malware.testing.google.test/testing/malware/测试了我的连接,然后它也返回空。

class Malicious extends AsyncTask<String, Void, Wrapper> {
    private OnTaskCompleted listener;

    public Malicious(OnTaskCompleted listener){
        this.listener=listener;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    protected Wrapper doInBackground(String... args) {
        String postURL = "https://safebrowsing.googleapis.com/v4/threatMatches:find?key=APIKEY";

        String requestBody = "{" +
                "    \"client\": {" +
                "      \"clientId\":      \"twittersentidetector\"," +
                "      \"clientVersion\": \"1.0\"" +
                "    }," +
                "    \"threatInfo\": {" +
                "      \"threatTypes\":      [\"MALWARE\", \"SOCIAL_ENGINEERING\"]," +
                "      \"platformTypes\":    [\"ANY_PLATFORM\"]," +
                "      \"threatEntryTypes\": [\"URL\"]," +
                "      \"threatEntries\": [" +
                "        {\"url\": \"" + args[0] + "\"}," +
                "      ]" +
                "    }" +
                "  }";



        URL url;
        StringBuffer response = new StringBuffer();
        try {
            url = new URL(postURL);
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("invalid url");
        }

        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(false);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("User-Agent", USER_AGENT);
            conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
            try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
                byte[] b = requestBody.getBytes();
                wr.write(b);
                wr.flush();
                wr.close();
            }

            // handle the response
            int status = conn.getResponseCode();
            if (status != 200) {
                throw new IOException("Post failed with error code " + status);
            } else {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }

            //Here is your json in string format
            String responseJSON = response.toString();
            Wrapper w = new Wrapper();
            w.responce = responseJSON;
            w.url = args[0];

            return w;
        }





    }

    @Override
    protected void onPostExecute(Wrapper xml) {

        if(xml.responce.length()==0){
            showtoast("safe");
        }
        else{
            showtoast("not safe");
            listener.onTaskCompleted(xml.url);
        }

    }

}

For test url also it shows safe.The api panel shows requests are made,I don't know what I'm doing wrong, I'm using the V4 API the most recent version but no matter what URL use, always show it's safe.对于测试 url,它也显示安全。api 面板显示请求已发出,我不知道我做错了什么,我使用的是最新版本的 V4 API,但无论使用什么 URL,始终显示它是安全的.

I set up this basic curl script which was only returning empty brackets {} Searching around the consensus seemed to be this was because the sites I was looking up were not malicious.我设置了这个基本的 curl 脚本,它只返回空括号 {} 围绕共识搜索似乎是因为我正在查找的站点不是恶意的。 Only the example you provided( http://malware.testing.google.test/testing/malware/ ) got me results.只有您提供的示例 ( http://malware.testing.google.test/testing/malware/ ) 给了我结果。 I'm still not convinced this is working properly, but here's the script.我仍然不相信这可以正常工作,但这是脚本。 Hope it helps.希望能帮助到你。 (Replace API_KEY with your api key) (将 API_KEY 替换为您的 api 密钥)

<\\?php <\\?php

$parameters = array( 'client' => array('clientId'=>'Evolved Marketing', 'clientVersion'=>'1.5.2'), 'threatInfo' => array('threatTypes'=>array('MALWARE', 'SOCIAL_ENGINEERING'), 'platformTypes'=>array('ANY_PLATFORM'), 'threatEntryTypes'=>array('URL'), 'threatEntries'=>array(array('url'=>' http://malware.testing.google.test/testing/malware/ ') )), ); $parameters = array( 'client' => array('clientId'=>'Evolved Marketing', 'clientVersion'=>'1.5.2'), 'threatInfo' => array('threatTypes'=>array('MALWARE ', 'SOCIAL_ENGINEERING'), 'platformTypes'=>array('ANY_PLATFORM'), 'threatEntryTypes'=>array('URL'), 'threatEntries'=>array(array('url'=>' http://恶意软件.testing.google.test/testing/malware/ ') )), );

$json = json_encode($parameters); $json = json_encode($parameters);

$ch = curl_init(); $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, ' https://safebrowsing.googleapis.com/v4/threatMatches:find?key=API_KEY '); curl_setopt($ch, CURLOPT_URL, ' https://safebrowsing.googleapis.com/v4/threatMatches:find?key=API_KEY ');

curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_POSTFIELDS, $json); curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

$response = curl_exec($ch); $response = curl_exec($ch);

print_r($response);打印_r($响应);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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