0,0 → 1,131 |
import java.io.IOException; |
|
import org.apache.http.HttpResponse; |
import org.apache.http.HttpVersion; |
import org.apache.http.client.ClientProtocolException; |
import org.apache.http.client.ResponseHandler; |
import org.apache.http.client.methods.HttpGet; |
import org.apache.http.client.methods.HttpPost; |
import org.apache.http.conn.ClientConnectionManager; |
import org.apache.http.conn.scheme.PlainSocketFactory; |
import org.apache.http.conn.scheme.Scheme; |
import org.apache.http.conn.scheme.SchemeRegistry; |
import org.apache.http.conn.ssl.SSLSocketFactory; |
import org.apache.http.impl.client.BasicResponseHandler; |
import org.apache.http.impl.client.DefaultHttpClient; |
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; |
import org.apache.http.params.BasicHttpParams; |
import org.apache.http.params.HttpConnectionParams; |
import org.apache.http.params.HttpParams; |
import org.apache.http.params.HttpProtocolParams; |
|
import android.util.Log; |
|
public class WebClient { |
private static final String TAG = "WebClient"; |
private static final DefaultHttpClient sClient; |
|
static { |
final HttpParams params = new BasicHttpParams(); |
|
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); |
HttpProtocolParams.setContentCharset(params, "UTF-8"); |
|
HttpConnectionParams.setStaleCheckingEnabled(params, false); |
HttpConnectionParams.setConnectionTimeout(params, 20 * 1000); |
HttpConnectionParams.setSoTimeout(params, 20 * 1000); |
HttpConnectionParams.setSocketBufferSize(params, 8192); |
|
SchemeRegistry schemeRegistry = new SchemeRegistry(); |
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); |
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); |
|
ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); |
sClient = new DefaultHttpClient(manager, params); |
} |
|
/** |
* Grab the source code for a given URL, returns null otherwise. |
* Method: GET |
* |
* @param url The URL we want to read |
* @return The Source code read from this URL (or null) |
*/ |
public static String readSource(String url) { |
HttpGet get = new HttpGet(url); |
|
try { |
ResponseHandler<String> handler = new BasicResponseHandler(); |
String body = sClient.execute(get, handler); |
return body; |
} catch (ClientProtocolException e) { |
Log.e(TAG, e.getMessage()); |
return null; |
} catch (IOException e) { |
Log.e(TAG, e.getMessage()); |
return null; |
} |
} |
|
/** |
* Grab the source code for a given URL, sending POST data specified. |
* Method: POST |
* |
* @param post The HttpPost object for the client to execute |
* @return The source code read from this URL (or null) |
*/ |
public static String readSource(HttpPost post) { |
try { |
ResponseHandler<String> handler = new BasicResponseHandler(); |
String body = sClient.execute(post, handler); |
return body; |
} catch (ClientProtocolException e) { |
Log.e(TAG, e.getMessage()); |
return null; |
} catch (IOException e) { |
Log.e(TAG, e.getMessage()); |
return null; |
} |
} |
|
/** |
* Return a responses status code as an Integer. Returns 0 on a caught Exception |
* Method: GET |
* |
* @param url The URL we're requesting |
* @return The responses status code (ie: 401, 404, 200) |
*/ |
public static int getStatusCode(String url) { |
HttpGet get = new HttpGet(url); |
try { |
HttpResponse response = sClient.execute(get); |
return response.getStatusLine().getStatusCode(); |
} catch (ClientProtocolException e) { |
Log.e(TAG, e.getMessage()); |
return 0; |
} catch (IOException e) { |
Log.e(TAG, e.getMessage()); |
return 0; |
} |
} |
|
/** |
* Return a responses status code as an Integer. Returns 0 on a caught Exception |
* Method: POST |
* |
* @param post The HttpPost object for the client to execute |
* @return The responses status code (ie: 401, 404, 200) |
*/ |
public static int getStatusCode(HttpPost post) { |
try { |
HttpResponse response = sClient.execute(post); |
return response.getStatusLine().getStatusCode(); |
} catch (ClientProtocolException e) { |
Log.e(TAG, e.getMessage()); |
return 0; |
} catch (IOException e) { |
Log.e(TAG, e.getMessage()); |
return 0; |
} |
} |
} |
Property changes: |
Added: svn:mime-type |
## -0,0 +1 ## |
+text/plain |
\ No newline at end of property |