List of usage examples for org.apache.commons.httpclient HttpConnection setSocketTimeout
public void setSocketTimeout(int timeout) throws SocketException, IllegalStateException
SO_TIMEOUT
value directly on the underlying Socket socket . From source file:org.aksonov.mages.connection.ConnectionManager.java
/** * Read post./*www .java 2 s . c o m*/ * * @param host the host * @param servlet the servlet * @param formData the form data * @param entity the entity * @param reader the reader * * @return the object * * @throws Exception the exception */ public static Object readPost(String host, String servlet, String formData, RequestEntity entity, DataReader reader) throws Exception { HttpConnection connection = getConnection(host); if (connection == null) { throw new IllegalArgumentException("Null connection"); } synchronized (connection) { PostMethod postMethod = null; try { //Log.d("ConnectionManager", "Open connection"); connection.open(); //Log.d("ConnectionManager", "Set timeout"); connection.setSocketTimeout(TIMEOUT); postMethod = new PostMethod(host + "/" + servlet + "?" + formData); postMethod.setRequestEntity(entity); //postMethod.setRequestHeader("Transfer-encoding", "base64"); //postMethod.setRequestHeader("Content-type", "application/octet-stream"); postMethod.execute(new HttpState(), connection); InputStream response = postMethod.getResponseBodyAsStream(); DataInputStream stream = new DataInputStream(response); Object data = reader.read(stream); return data; } catch (Exception e) { Log.e("ConnectionManager", e); throw e; } finally { if (postMethod != null) postMethod.releaseConnection(); connection.close(); connectionManager.releaseConnection(connection); } } }
From source file:org.aksonov.mages.connection.ConnectionManager.java
/** * Read get.//from w ww . j a v a 2 s . c o m * * @param host the host * @param servlet the servlet * @param formData the form data * @param reader the reader * * @return the object * * @throws Exception the exception */ public static Object readGet(String host, String servlet, String formData, DataReader reader) throws Exception { HttpConnection connection = getConnection(host); if (connection == null) { throw new IllegalArgumentException("Null connection"); } synchronized (connection) { GetMethod getMethod = null; try { Log.d("ConnectionManager", "Open connection"); connection.open(); connection.setSocketTimeout(TIMEOUT); String url = host + "/" + servlet + "?" + formData; Log.d("ConnectionManager", "GET " + url); getMethod = new GetMethod(url); //getMethod.setRequestHeader("Transfer-encoding", "base64"); //getMethod.setRequestHeader("Content-type", "application/octet-stream"); getMethod.execute(new HttpState(), connection); InputStream response = getMethod.getResponseBodyAsStream(); DataInputStream stream = new DataInputStream(response); Object data = reader.read(stream); return data; } catch (Exception e) { Log.e("ConnectionManager", e); throw e; } finally { if (getMethod != null) { getMethod.releaseConnection(); } connection.close(); connectionManager.releaseConnection(connection); } } }