posts - 431,  comments - 344,  trackbacks - 0

两天前写了个通过上海中科院有机化学研究所网站的翻译功能来翻译化合物名称的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();
  }

posted on 2009-07-30 16:48 周锐 阅读(1660) 评论(0)  编辑  收藏 所属分类: ApacheJava

只有注册用户登录后才能发表评论。


网站导航: