apache httpclient



httpclient  apache 开源http框架


post 请求


  1. HttpClient client = new DefaultHttpClient(); 
  2.   HttpPost httpPost = new HttpPost(url + "ConnDBfind"); 
  3.   List<Object[]> list = null; 
  4.   List<NameValuePair> params = new ArrayList<NameValuePair>(); 
  5.   params.add(new BasicNameValuePair("sql", sql)); 
  6.   httpPost.getParams().setParameter( 
  7.     HttpConnectionParams.CONNECTION_TIMEOUT, 10 * 1000); 
  8.   Gson gson = new Gson(); 
  9.   try { 
  10.    httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
  11.    HttpResponse response = client.execute(httpPost); 
  12.    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
  13.     String result = EntityUtils.toString(response.getEntity(), 
  14.       "utf-8"); 
  15.     list = gson.fromJson(result, new TypeToken<List<Object[]>>() {}.getType()); 
  16.     for(int i=0;i<list.size();i++){ 
  17.      for(int j=0;j<list.get(i).length;j++){ 
  18.       try { 
  19.        list.get(i)[j].toString(); 
  20.       } catch (Exception e) { 
  21.        list.get(i)[j]=0; 
  22.       } 
  23.      } 
  24.     } 
  25.      
  26.    } 
  27.   } catch (Exception e) { 
  28.    list = null; 
  29.   } 
  30.   return list;



get 请求






附上:

httpclient-4.3.4.jarhttpcore-4.3.2.jar 

附件列表

     

    posted on 2017-03-21 17:27  signheart  阅读(279)  评论(0编辑  收藏  举报

    导航