If you need to set proxy address on Java HttpURLConnection. Do something like below.
You can more details from below Java network programming books from O'reilly. I highly recommend the below book.

public static String getString(String urlStr) throws Exception
{
HttpURLConnection connection = null;
InputStream is = null;
try
{
URI uri = new URI(urlStr);
// settin proxy
String proxyHost = ""; // your proxy serever address
int proxyPort = 8080; // your proxy port
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
// open connection with passing Proxy Object
connection = (HttpURLConnection)uri.toURL().openConnection(proxy);
connection.connect();
is = connection.getInputStream();
String content = toString(is);
return content;
}
finally{
closeQuietly(is);
if(connection != null)
{
connection.disconnect();
}
}
}
private static String toString(InputStream is) throws IOException
{
byte[] buf = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for(int len = is.read(buf); len > 0; len = is.read(buf))
{
baos.write(buf, 0, len);
}
return baos.toString();
}
private static void assertNotNull(Object object, String message)
{
if(object == null)
{
throw new IllegalArgumentException(message);
}
}
private static void closeQuietly(Closeable closeable)
{
if(closeable != null)
{
try {
closeable.close();
} catch (IOException e) {
}
}
}
You can more details from below Java network programming books from O'reilly. I highly recommend the below book.
コメント