2017-06-15 75 views
0

我發現在這個link 此示例代碼我代替我的鑰匙{subscription key}但結果總是:在java的使用Bing圖片API搜索圖片

{ "statusCode": 404, "message": "Resource not found" } 

我很困惑與該行:

StringEntity reqEntity = new StringEntity("{body}"); 

我應該用什麼替換"{body}"?從https://dev.cognitive.microsoft.com的例子 全部代碼如下

// // This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/) 
import java.net.URI; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.utils.URIBuilder; 
import org.apache.http.impl.client.HttpClients; 
import org.apache.http.util.EntityUtils; 

public class JavaSample 
{ 
    public static void main(String[] args) 
    { 
     HttpClient httpclient = HttpClients.createDefault(); 

     try 
     { 
      URIBuilder builder = new URIBuilder("https://api.cognitive.microsoft.com/bing/v5.0/images/search"); 

      builder.setParameter("q", "cats"); 

      URI uri = builder.build(); 
      HttpPost request = new HttpPost(uri); 
      request.setHeader("Content-Type", "multipart/form-data"); 
      request.setHeader("Ocp-Apim-Subscription-Key", "{subscription key}"); 


      // Request body 
      StringEntity reqEntity = new StringEntity("{body}"); 
      request.setEntity(reqEntity); 

      HttpResponse response = httpclient.execute(request); 
      HttpEntity entity = response.getEntity(); 

      if (entity != null) 
      { 
       System.out.println(EntityUtils.toString(entity)); 
      } 
     } 
     catch (Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 
    } 
} 

回答