public class SampleHttp extends Object
The code below illustrates how to get the Http/Https proxy settings. First, HttpFactory
is used to create an instance of the HttpProperties
class. Next, invoke the
methods:
- HttpProperties.getHttpProxyHost()
- HttpProperties.getHttpProxyPort()
- HttpProperties.getHttpsProxyHost()
- HttpProperties.getHttpsProxyPort()
to get the expected proxy settings.
public class SampleHttpCode { public void InvokeMeToSendHttpRequestViaProxy(final String httpUrl) { // Http Proxy Sample HttpProperty httpProperty = HttpFactory.createHttpProperty(); HttpURLConnection connection; final String httpProxyHost = httpProperty.getHttpProxyHost(); final Integer httpProxyPort = httpProperty.getHttpProxyPort(); try { final URL url = new URL(httpUrl); if (httpProxyHost != null && httpProxyPort != null) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxyHost, httpProxyPort.intValue())); connection = (HttpURLConnection)url.openConnection(proxy); } else { connection = (HttpURLConnection)url.openConnection(); } System.out.println(connection.getResponseCode() + " : " + connection.getResponseMessage()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { showContent(connection); } } catch (MalformedURLException e) { System.out.println("Invalid URL: " + e.getMessage()); } catch (IOException e) { System.out.println("Failed to make http reuqest: " + e.getMessage()); } // Https Proxy Sample final String httpsProxyHost = httpProperty.getHttpsProxyHost(); final Integer httpsProxyPort = httpProperty.getHttpsProxyPort(); try { final URL url = new URL(httpUrl); if (httpsProxyHost != null && httpsProxyPort != null) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpsProxyHost, httpsProxyPort.intValue())); connection = (HttpURLConnection)url.openConnection(proxy); } else { connection = (HttpURLConnection)url.openConnection(); } System.out.println(connection.getResponseCode() + " : " + connection.getResponseMessage()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { showContent(connection); } } catch (MalformedURLException e) { System.out.println("Invalid URL: " + e.getMessage()); } catch (IOException e) { System.out.println("Failed to make http reuqest: " + e.getMessage()); } } private static void showContent(HttpURLConnection uc) throws IOException { final InputStream input = uc.getInputStream(); final InputStreamReader isr = new InputStreamReader(input); final BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } }
Copyright © 2023 Avaya. All rights reserved.