2016-09-08 3 views
0

자바를 사용하여 프록시로 http 요청을 만들고 정수 변수에서 응답 코드를 얻으려고합니다.자바를 사용하여 프록시를 통해 요청한 상태 코드를 확인하십시오.

내가 사용하는 방법은 다음과 같습니다 나는 프록시 매개 변수를 전달하고 있지만 요청을하는 동안 그것을 사용하는 방법을 잘 모르겠습니다

public static int getResponseCode(String urlString, Proxy p) throws MalformedURLException, IOException{ 
URL url = new URL(urlString); 
HttpResponse urlresp = new DefaultHttpClient().execute(new HttpGet(urlString)); 
int resp_Code = urlresp.getStatusLine().getStatusCode(); 
return resp_Code; 
} 

. 온라인에서 리소스를 찾으려고했지만 적절한 솔루션을 찾을 수 없었습니다.

나는 아래의 솔루션을 시도했지만 여기에 응답 코드하는 방법을 확신했다 :

HttpHost proxy = new HttpHost("proxy.com", 80, "http"); 
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); 
CloseableHttpClient httpclient = HttpClients.custom() 
       .setRoutePlanner(routePlanner) 
       .build(); 

답변

1

당신이 시도 할 수 :

URL url = new URL("http://www.myurl.com"); 

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.com", 8080)); 

// IF NEED AUTH   
//  Authenticator authenticator = new Authenticator() { 
//   public PasswordAuthentication getPasswordAuthentication() { 
//    return (new PasswordAuthentication("username", 
//      "password".toCharArray())); 
//   } 
//  }; 
//  Authenticator.setDefault(authenticator); 

     HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy); 
     conn.setRequestMethod("GET"); 
     conn.connect(); 

     int code = conn.getResponseCode(); 

     System.out.println(code);