opeenConnection - URL에서 connection 객체 생성
transient URLStreamHandler handler;
public URLConnection openConnection() throws java.io.IOException {
return handler.openConnection(this);
}
URL에 대한 프로토콜 핸들러의 URLStreamHandler.openConnection(URL) 메소드를 호출할 때마다 URLConnection의 새 인스턴스가 생성됨
URLConnection 인스턴스는 생성 시 실제 네트워크 연결을 설정하지 않으며, URLConnection.connect()를 호출할 때만 발생
HTTP의 경우 HttpURLConnection이 반환되고 JAR의 경우 JarURLConnection이 반환
추가 - openConnection(Proxy proxy)
지정된 프록시를 통해 연결이 이루어진다는 점을 제외하면 openConnection()과 동일. 프록시를 지원하지 않는 프로토콜 핸들러는 프록시 매개변수를 무시하고 정상적인 연결을 생성. 기본 ProxySelector 설정이 선점됨.
public URLConnection openConnection(Proxy proxy) throws java.io.IOException {
if (proxy == null) {
throw new IllegalArgumentException("proxy can not be null");
}
// Create a copy of Proxy as a security measure
Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy);
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (p.type() != Proxy.Type.DIRECT && sm != null) {
InetSocketAddress epoint = (InetSocketAddress) p.address();
if (epoint.isUnresolved())
sm.checkConnect(epoint.getHostName(), epoint.getPort());
else
sm.checkConnect(epoint.getAddress().getHostAddress(),
epoint.getPort());
}
return handler.openConnection(this, p);
}
설정 매개변수와 general request properties 조작
remote 객체에 대한 실제 연결이 이루어지면 connect 메서드 사용
connect
public abstract void connect() throws IOExcpetion;
remote 객체가 사용 가능한 상태가 되면 헤더 필드와 remote 객체의 contents에 접근 가능
set method
setAllowUserInteraction
setDoInput
setDoOutput
setIfModifiedSince
setUseCaches
public void setUseCaches(boolean usecaches) {
checkConnected();
useCaches = usecaches;
}
public void setDefaultUseCaches(boolean defaultusecaches) {
defaultUseCaches = defaultusecaches;
}
public static void setDefaultUseCaches(String protocol, boolean defaultVal) {
protocol = protocol.toLowerCase(Locale.US);
defaultCaching.put(protocol, defaultVal);
}
setRequestProperty
public void setRequestProperty(String key, String value) {
checkConnected();
if (key == null)
throw new NullPointerException ("key is null");
if (requests == null)
requests = new MessageHeader();
requests.set(key, value);
}
Get method