2012-07-21 3 views

답변

2

, 당신은 apache http components를 사용할 수 있습니다. 이것은 apache http client라고 불리곤했습니다.

는 여기에 내가 요청을 위해 사용하고 일부 샘플 코드입니다. 요청 전후에 타임 스탬프를 잡을 수 있습니다.

HttpClient httpclient = new DefaultHttpClient(); 
    URIBuilder builder = new URIBuilder(); 
    URI uri = null; 
    HttpEntity entity = null; 
    InputStream inputStream = null; 
    JSONObject jsonObject = null; 
    URL url = this.getClass().getClassLoader().getResource("json" + File.separator + "newInstall.json"); 
    try { 
     String json = FileUtil.readFile(url.getPath()); 
     builder.setScheme("http").setHost(host).setPort(8080).setPath(basePath + authResource) 
       .setParameter("sig", "sig").setParameter("params", json); 
     uri = builder.build(); 
     log.info("uri: {}", uri); 
     HttpGet httpget = new HttpGet(uri); 
     HttpResponse response = httpclient.execute(httpget); 
     entity = response.getEntity(); 
     if (entity != null) { 
      inputStream = entity.getContent(); 
      String jsonString = IOUtils.toString(inputStream, "UTF-8"); 
      jsonObject = new JSONObject(jsonString); 
      log.info("auth response: {}", jsonString); 
     } 
    } catch (Exception e) { 
     log.error("error", e); 
    }finally { 
     if(inputStream != null) { 
      try { 
       inputStream.close(); 
      }catch(Exception ex) { 
       log.error("error closing input stream", ex); 
      } 
     } 
    } 
관련 문제