兩天前寫了個通過上海中科院有機化學研究所網站的翻譯功能來翻譯化合物名稱的Python代碼,今天看到了HttpClient也比較好用,所以就寫了個,代碼如下:
HttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost("http://202.127.145.134/scdb/translate/translate.asp");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("eng2chi", "BISPYROQUINE")); //參數
try {
method.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); //設置參數給Post
HttpResponse response = httpclient.execute(method);
HttpEntity entity = response.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "gb2312"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
if (entity != null) {
entity.consumeContent();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}