Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 5 | PointedEar | 1 | import java.io.IOException; |
| 2 | |||
| 3 | import org.apache.http.HttpResponse; |
||
| 4 | import org.apache.http.HttpVersion; |
||
| 5 | import org.apache.http.client.ClientProtocolException; |
||
| 6 | import org.apache.http.client.ResponseHandler; |
||
| 7 | import org.apache.http.client.methods.HttpGet; |
||
| 8 | import org.apache.http.client.methods.HttpPost; |
||
| 9 | import org.apache.http.conn.ClientConnectionManager; |
||
| 10 | import org.apache.http.conn.scheme.PlainSocketFactory; |
||
| 11 | import org.apache.http.conn.scheme.Scheme; |
||
| 12 | import org.apache.http.conn.scheme.SchemeRegistry; |
||
| 13 | import org.apache.http.conn.ssl.SSLSocketFactory; |
||
| 14 | import org.apache.http.impl.client.BasicResponseHandler; |
||
| 15 | import org.apache.http.impl.client.DefaultHttpClient; |
||
| 16 | import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; |
||
| 17 | import org.apache.http.params.BasicHttpParams; |
||
| 18 | import org.apache.http.params.HttpConnectionParams; |
||
| 19 | import org.apache.http.params.HttpParams; |
||
| 20 | import org.apache.http.params.HttpProtocolParams; |
||
| 21 | |||
| 22 | import android.util.Log; |
||
| 23 | |||
| 24 | public class WebClient { |
||
| 25 | private static final String TAG = "WebClient"; |
||
| 26 | private static final DefaultHttpClient sClient; |
||
| 27 | |||
| 28 | static { |
||
| 29 | final HttpParams params = new BasicHttpParams(); |
||
| 30 | |||
| 31 | HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); |
||
| 32 | HttpProtocolParams.setContentCharset(params, "UTF-8"); |
||
| 33 | |||
| 34 | HttpConnectionParams.setStaleCheckingEnabled(params, false); |
||
| 35 | HttpConnectionParams.setConnectionTimeout(params, 20 * 1000); |
||
| 36 | HttpConnectionParams.setSoTimeout(params, 20 * 1000); |
||
| 37 | HttpConnectionParams.setSocketBufferSize(params, 8192); |
||
| 38 | |||
| 39 | SchemeRegistry schemeRegistry = new SchemeRegistry(); |
||
| 40 | schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); |
||
| 41 | schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); |
||
| 42 | |||
| 43 | ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); |
||
| 44 | sClient = new DefaultHttpClient(manager, params); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Grab the source code for a given URL, returns null otherwise. |
||
| 49 | * Method: GET |
||
| 50 | * |
||
| 51 | * @param url The URL we want to read |
||
| 52 | * @return The Source code read from this URL (or null) |
||
| 53 | */ |
||
| 54 | public static String readSource(String url) { |
||
| 55 | HttpGet get = new HttpGet(url); |
||
| 56 | |||
| 57 | try { |
||
| 58 | ResponseHandler<String> handler = new BasicResponseHandler(); |
||
| 59 | String body = sClient.execute(get, handler); |
||
| 60 | return body; |
||
| 61 | } catch (ClientProtocolException e) { |
||
| 62 | Log.e(TAG, e.getMessage()); |
||
| 63 | return null; |
||
| 64 | } catch (IOException e) { |
||
| 65 | Log.e(TAG, e.getMessage()); |
||
| 66 | return null; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Grab the source code for a given URL, sending POST data specified. |
||
| 72 | * Method: POST |
||
| 73 | * |
||
| 74 | * @param post The HttpPost object for the client to execute |
||
| 75 | * @return The source code read from this URL (or null) |
||
| 76 | */ |
||
| 77 | public static String readSource(HttpPost post) { |
||
| 78 | try { |
||
| 79 | ResponseHandler<String> handler = new BasicResponseHandler(); |
||
| 80 | String body = sClient.execute(post, handler); |
||
| 81 | return body; |
||
| 82 | } catch (ClientProtocolException e) { |
||
| 83 | Log.e(TAG, e.getMessage()); |
||
| 84 | return null; |
||
| 85 | } catch (IOException e) { |
||
| 86 | Log.e(TAG, e.getMessage()); |
||
| 87 | return null; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Return a responses status code as an Integer. Returns 0 on a caught Exception |
||
| 93 | * Method: GET |
||
| 94 | * |
||
| 95 | * @param url The URL we're requesting |
||
| 96 | * @return The responses status code (ie: 401, 404, 200) |
||
| 97 | */ |
||
| 98 | public static int getStatusCode(String url) { |
||
| 99 | HttpGet get = new HttpGet(url); |
||
| 100 | try { |
||
| 101 | HttpResponse response = sClient.execute(get); |
||
| 102 | return response.getStatusLine().getStatusCode(); |
||
| 103 | } catch (ClientProtocolException e) { |
||
| 104 | Log.e(TAG, e.getMessage()); |
||
| 105 | return 0; |
||
| 106 | } catch (IOException e) { |
||
| 107 | Log.e(TAG, e.getMessage()); |
||
| 108 | return 0; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Return a responses status code as an Integer. Returns 0 on a caught Exception |
||
| 114 | * Method: POST |
||
| 115 | * |
||
| 116 | * @param post The HttpPost object for the client to execute |
||
| 117 | * @return The responses status code (ie: 401, 404, 200) |
||
| 118 | */ |
||
| 119 | public static int getStatusCode(HttpPost post) { |
||
| 120 | try { |
||
| 121 | HttpResponse response = sClient.execute(post); |
||
| 122 | return response.getStatusLine().getStatusCode(); |
||
| 123 | } catch (ClientProtocolException e) { |
||
| 124 | Log.e(TAG, e.getMessage()); |
||
| 125 | return 0; |
||
| 126 | } catch (IOException e) { |
||
| 127 | Log.e(TAG, e.getMessage()); |
||
| 128 | return 0; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |