학습자료/Java
2014. 4. 5. 23:27
Solution:
Following example shows how to find proxy settings & create a proxy connection on a system using put method of systemSetting & getResponse method of HttpURLConnection class.
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Properties;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
public class Main{
public static void main(String s[])
throws Exception{
try {
Properties systemSettings =
System.getProperties();
systemSettings.put("proxySet", "true");
systemSettings.put("http.proxyHost",
"proxy.mycompany1.local");
systemSettings.put("http.proxyPort", "80");
URL u = new URL("http://www.google.com");
HttpURLConnection con = (HttpURLConnection)
u.openConnection();
System.out.println(con.getResponseCode() +
" : " + con.getResponseMessage());
System.out.println(con.getResponseCode() ==
HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
e.printStackTrace();
System.out.println(false);
}
System.setProperty("java.net.useSystemProxies",
"true");
Proxy proxy = (Proxy) ProxySelector.getDefault().
select(new URI("http://www.yahoo.com/")).iterator().
next();;
System.out.println("proxy hostname : " + proxy.type());
InetSocketAddress addr = (InetSocketAddress)
proxy.address();
if (addr == null) {
System.out.println("No Proxy");
}
else {
System.out.println("proxy hostname : "
+ addr.getHostName());
System.out.println("proxy port : "
+ addr.getPort());
}
}
}
|
Result:
The above code sample will produce the following result.
200 : OK true proxy hostname : HTTP proxy hostname : proxy.mycompany1.local proxy port : 80 |
'학습자료 > Java' 카테고리의 다른 글
| [java] 파일 실행 (0) | 2015.01.08 |
|---|---|
| [java] html 파싱, jsoup 예제 (0) | 2015.01.08 |
| Installing software' has encountered a problem. 이클립스 문제 (0) | 2013.05.23 |
| [java] java.library.path - linux, eclipse (0) | 2013.03.19 |
| [Java] messagePack(rpc 구현) (0) | 2013.02.27 |
