0
나는 그것이 프록시를 사용하기 때문에, 그것은 작동하지 않습니다 내 회사의 네트워크에서이를 실행하고 때 GET 요청을

설정 정적 프록시 HttpClient를

service.logger.info("Testing GET request with param= " + "test"); 
    service.logger.info(service.getSuggestions(searchTerms1)); 
    service.logger.info("Testing GET request with param= " + "weibo"); 
    service.logger.info(service.getSuggestions(searchTerms2)); 

을 보내 HttpClient를 사용하여 내 서비스를 테스트하는 테스트를 작성하려는

.

내 서비스 코드를 변경하고 싶지 않으므로 설정 프록시를 바꿀 수있는 방법을 찾고 내 HTTP 클라이언트가이 프록시를 사용하여 요청을 보내 게되기를 바랍니다. 그렇게 할 수있는 방법이 있습니까? 나는 이클립스를 사용하고있다.

다음은 네트워크에서 결과를 얻으려는 요청을 보내는 방법입니다.

private final String getResultFromURL(String url) throws ClientProtocolException, IOException { 
      RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1 * 1000).build(); 
      HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); 
      HttpGet request = new HttpGet(url); 
      // add request header 
      request.addHeader("User-Agent", USER_AGENT_MOZILLIA); 

      HttpResponse response = client.execute(request); 

      StringBuffer result = new StringBuffer(); 
      try(BufferedReader rd = new BufferedReader(
             new InputStreamReader(response.getEntity().getContent()))){ 
       String line = ""; 
       while ((line = rd.readLine()) != null) { 
        result.append(line); 
       } 
      } 
      finally{ 
       request.releaseConnection(); 
       EntityUtils.consume(response.getEntity()); 
      } 

      logger.debug("----result from URL:"+url +" "+result); 
      return result.toString(); 
     } 

답변

0

다음과 같이 JAVA_OPTIONS에서 프록시를 설정하여 그것을 할 수 있습니다. * nix에서 스크립트에서 윈도우

set _JAVA_OPTIONS=-Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber -Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword 

에서

export _JAVA_OPTIONS="-Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber -Dhttp.proxyUser=someUserName -Dhttp.proxyPassword=somePassword"