Code example for HttpURLConnection

Methods: setConnectTimeout, setReadTimeout, setRequestMethod, setRequestProperty

0
		} 
	} 
	 
	public void setRequestProperty(String key, String value) throws IOException {
		connect(); 
		this.connection.setRequestProperty(key, value);
	} 
	 
	//invoke at any time 
	public String getRequestMethod() {
		return this.requestMethod;
	} 
	 
	public String getRequestProperty(String key) {
		try { 
			connect(); 
		} catch (IOException e) {
			return ""; 
		} 
		return this.connection.getRequestProperty(key);
	} 
	 
	public String getURL() {
		return this.urlString;
	} 
	 
	public String getQuery() {
		return this.url.getQuery();
	} 
	 
	public int getPort() { 
		return this.url.getPort();
	} 
	 
	public String getHost() {
		return this.url.getHost();
	} 
	 
	public String getProtocol() {
		return this.url.getProtocol();
	} 
		 
	public String getFile() {
		return this.url.getFile();
	} 
		 
	public String getRef() {
		return this.url.getRef();
	} 
	 
	//these calls force transition to connected state 
	public DataInputStream openDataInputStream() throws IOException {
		return new DataInputStream(openInputStream());
	} 
 
	public InputStream openInputStream() throws IOException {
		connect(); 
		if (this.connection.getResponseCode() >= 400) {
			this.input = this.connection.getErrorStream();
		} else { 
			this.input = this.connection.getInputStream();
		} 
		return this.input;
	} 
	 
	public long getLength() { 
		try { 
			connect(); 
		} catch (IOException ex) {
			return 0; 
		} 
		return this.connection.getContentLength();
	} 
	 
	public String getType() {
		try { 
			connect(); 
		} catch (IOException ex) {
			return ""; 
		} 
		return this.connection.getContentType();
	} 
	 
	public String getEncoding() {
		try { 
			connect(); 
		} catch (IOException ex) {
			return ""; 
		} 
		return this.connection.getContentEncoding();
	} 
	 
	public String getHeaderField(String name) throws IOException {
		connect(); 
		return this.connection.getHeaderField(name);
	} 
	 
	public String getHeaderField(int n) throws IOException {
		connect(); 
		return this.connection.getHeaderField(n);
	} 
	 
	public int getResponseCode()  throws IOException { 
		connect(); 
		int responseCode = this.connection.getResponseCode();
		return responseCode;
	} 
	 
	public String getResponseMessage() throws IOException {
		connect(); 
		return this.connection.getResponseMessage();
	} 
	 
	public int getHeaderFieldInt(String name, int def) throws IOException {
		connect(); 
		return this.connection.getHeaderFieldInt(name, def);
	} 
 
	public long getHeaderFieldDate(String name,	long def) throws IOException {
		connect(); 
		return this.connection.getHeaderFieldDate(name, def);
	} 
 
	public String getHeaderFieldKey(int n) throws IOException {
		connect(); 
		return this.connection.getHeaderFieldKey(n);
	} 
	 
	public long getDate() throws IOException { 
		connect(); 
		return this.connection.getDate();
	} 
	 
	public long getExpiration() throws IOException { 
		connect(); 
		return this.connection.getExpiration();
	} 
	 
	public long getLastModified() throws IOException { 
		connect(); 
		return this.connection.getLastModified();
	} 
	 
	protected synchronized void connect() throws IOException { 
		if (this.state == STATE_CONNECTED) {
			if(this.connection == null) {
				throw new IOException("Invalid State. No connection in state STATE_CONNECTED");
			} 
			return; 
		} else { 
			this.state = STATE_CONNECTED;
		} 
	//	//#if polish.android.http.keepAlive != true 
	//		//#= System.setProperty("http.keepAlive", "false"); 
	//	//#endif 
		this.connection = (HttpURLConnection)this.url.openConnection();
		if (this.timeout > 0) {
			this.connection.setConnectTimeout(this.timeout);
			this.connection.setReadTimeout(this.timeout);
		} 
		this.connection.setRequestMethod(this.requestMethod);
	} 
	 
}