Code example for HttpURLConnection

Methods: getContentType, setConnectTimeout, setReadTimeout, setRequestMethod

0
		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);
	} 
	 
}