2016-03-30 16 views
0

私はhttpクライアントapache APIをテストし始めました。リクエストを送信し、virustotal APIへの応答を受け取るために必要です。私は彼らのウェブサイトからも理解されるようにapache httpClient APIを使用してfile + apiキーと他のパラメータを1回のリクエストとして送信するにはどうすればよいですか?

  1. APIキー値(各ユーザーに一意の値)
  2. ファイル自体:ウイルス総APIはPOSTリクエストのパラメータに必要です。たとえば、

>>> url = "https://www.virustotal.com/vtapi/v2/url/scan" 
>>> parameters = {"url": "http://www.virustotal.com", 
...    "apikey": "-- YOUR API KEY --"} 
>>> data = urllib.urlencode(parameters) 
>>> req = urllib2.Request(url, data) 

瞬間に、私は、JavaではなくPythonで同じことをやろうとしています。

CloseableHttpClient httpClient = HttpClientBuilder.create().build(); 
    //create post request 
    HttpPost request = new HttpPost("https://www.virustotal.com/vtapi/v2/file/scan"); 
    //http json header 
    request.addHeader("content-type", "application/json"); 

    String str = gson.toJson(param); 

    String fileName = UUID.randomUUID().toString() + ".txt"; 

    try { 
    //API key 
    StringEntity entity = new StringEntity(str); 
    Writer writer = new BufferedWriter(new FileWriter(fileName)); 
    writer.write(VirusDefinitionTest.malware()); 
    request.setEntity(entity); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
    FileBody fileBody = new FileBody(new File(fileName)); 

    builder.addTextBody("my_file", fileName); 
    HttpEntity entity = builder.build(); 
    request.setEntity(entity); 

    HttpResponse response; 
    try { 
    response = httpClient.execute(request); 

...

は、残念ながら、私はHTTP受信/ 1.1 403禁止:ここでの手順を通じて案内するためのコメント私のソースコードの一部です。明らかに、エンティティのどこかにエラーがありますが、それを行う方法がわかりません。どんな援助も深く歓迎されるだろう。

答えて

1

問題は、最初に "application/json"という明示的な "content-type"ヘッダーを追加し、最後にMuiltipartエンティティを送信するように見えます。すべてのパラメータとファイルをMuiltipartエンティティに追加する必要があります。

CloseableHttpClient httpClient = HttpClientBuilder.create().build(); 
//create post request 
HttpPost request = new HttpPost("https://www.virustotal.com/vtapi/v2/file/scan"); 
//http json header 
request.addHeader("content-type", "application/json"); 

String str = gson.toJson(param); 

String fileName = UUID.randomUUID().toString() + ".txt"; 

try { 
//API key 
StringEntity entity = new StringEntity(str); 
Writer writer = new BufferedWriter(new FileWriter(fileName)); 
writer.write(VirusDefinitionTest.malware()); 
// --> You set parameters here !!! 
request.setEntity(entity); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); 
FileBody fileBody = new FileBody(new File(fileName)); 

builder.addTextBody("my_file", fileName); 
HttpEntity entity = builder.build(); 
// --> You overwrite the parameters here !!! 
request.setEntity(entity); 

HttpResponse response; 
try { 
response = httpClient.execute(request); 
2

これは、Apache 4.5.2で私のために働いたのHttpClient:

CloseableHttpClient httpclient = HttpClients.createDefault(); 
try { 
    HttpPost httppost = new HttpPost("https://www.virustotal.com/vtapi/v2/file/scan"); 

    FileBody bin = new FileBody(new File("... the file here ...")); 
    // the API key here 
    StringBody comment = new StringBody("5ec8de.....", ContentType.TEXT_PLAIN); 

    HttpEntity reqEntity = MultipartEntityBuilder.create() 
      .addPart("apikey", comment) 
      .addPart("file", bin) 
      .build(); 

    httppost.setEntity(reqEntity); 

    System.out.println("executing request " + httppost.getRequestLine()); 
    CloseableHttpResponse response = httpclient.execute(httppost); 
    try { 
     System.out.println("----------------------------------------"); 
     System.out.println(response.getStatusLine()); 
     HttpEntity resEntity = response.getEntity(); 
     if (resEntity != null) { 
      System.out.println("ToString:" + EntityUtils.toString(resEntity)); 
     } 
     EntityUtils.consume(resEntity); 
    } finally { 
     response.close(); 
    } 
} finally { 
    httpclient.close(); 
} 

重要な部分を持っていたreqEntityを持つようにして、彼らはMuiltipartエンティティによって上書きされているので、今のパラメータは、送信されていません2つの具体的に名前が付けられたフィールド、"apikey"、および"file"。有効なAPIキーでこれを実行すると、APIからの期待される応答が得られます。

+0

このすべては、VirusTotal for Javaに最適です。 'EntityUtils.toString(resEntity)'はパズルの最後の部分でした。 – nettie

関連する問題