1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| public static void main(String[] args) throws IOException { String searchWord = "爬虫"; String defaultReqBody = "{\n" + " \"from\": \"zh\",\n" + " \"to\": \"en\",\n" + " \"reference\": \"\",\n" + " \"corpusIds\": [\n" + " \n" + " ],\n" + " \"qcSettings\": [\n" + " \"1\",\n" + " \"2\",\n" + " \"3\",\n" + " \"4\",\n" + " \"5\",\n" + " \"6\",\n" + " \"7\",\n" + " \"8\",\n" + " \"9\",\n" + " \"10\",\n" + " \"11\"\n" + " ],\n" + " \"needPhonetic\": false,\n" + " \"domain\": \"common\",\n" + " \"milliTimestamp\": 1725808602840\n" + "}"; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("https://fanyi.baidu.com/ait/text/translate"); JSONObject params = JSONObject.parseObject(defaultReqBody); params.put("query",searchWord); params.put("milliTimestamp",System.currentTimeMillis()); httpPost.setEntity(new StringEntity(params.toString(),"UTF-8")); httpPost.addHeader("Content-Type", "application/json");
System.out.println(params); CloseableHttpResponse response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity); System.out.println(result); }
|