Открыть спойлер
Закрыть спойлер
- package com.spc;
 - import java.io.*;
 - import javax.net.ssl.SSLHandshakeException;
 - import org.apache.http.*;
 - import org.apache.http.client.*;
 - import org.apache.http.conn.scheme.*;
 - import org.apache.http.conn.ssl.*;
 - import org.apache.http.impl.client.*;
 - import org.apache.http.impl.conn.tsccm.*;
 - import org.apache.http.params.*;
 - import org.apache.http.protocol.*;
 - import org.apache.http.client.methods.*;
 - import org.apache.http.entity.*;
 - public final class InternetSimpl {
 - HttpClient connection;
 - HttpGet hg;
 - HttpPost hp;
 - HttpPut hpt;
 - HttpResponse response;
 - public InternetSimpl() {
 - }
 - public static String USER_AGENT = "Mozilla/5.0 (Linux; U; Android 1.1; en-us;dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2";
 - private DefaultHttpClient getThreadSafeHttpClient() {
 - HttpParams params = new BasicHttpParams();
 - params.setParameter("http.useragent", USER_AGENT);
 - HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
 - HttpProtocolParams.setContentCharset(params, "UTF-8");
 - final SchemeRegistry registry = new SchemeRegistry();
 - registry.register(new Scheme("http", PlainSocketFactory
 - .getSocketFactory(), 80));
 - final SSLSocketFactory sslSocketFactory = SSLSocketFactory
 - .getSocketFactory();
 - sslSocketFactory
 - .setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
 - registry.register(new Scheme("https", sslSocketFactory, 443));
 - final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(
 - params, registry);
 - final DefaultHttpClient httpclient = new DefaultHttpClient(manager,
 - params);
 - // how to handle retries
 - final HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
 - public boolean retryRequest(final IOException exception,
 - final int executionCount, final HttpContext context) {
 - if (executionCount >= 5) {
 - // Do not retry if over max retry count
 - return false;
 - }
 - if (exception instanceof NoHttpResponseException) {
 - // Retry if the server dropped connection on us
 - return true;
 - }
 - if (exception instanceof SSLHandshakeException) {
 - // Do not retry on SSL handshake exception
 - return false;
 - }
 - final HttpRequest request = (HttpRequest) context
 - .getAttribute(ExecutionContext.HTTP_REQUEST);
 - final boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
 - if (idempotent) {
 - // Retry if the request is considered idempotent
 - return true;
 - }
 - return false;
 - }
 - };
 - httpclient.setHttpRequestRetryHandler(myRetryHandler);
 - return httpclient;
 - }
 - public String getResponseHeaders() throws Exception {
 - Header[] heders = response.getAllHeaders();
 - StringBuffer sb = new StringBuffer();
 - for (int i = 0; i < heders.length; i++) {
 - String key = heders[i].getName();
 - String value = heders[i].getValue();
 - if (key != null && value != null) {
 - sb.append(key + "=" + value + "\n");
 - }
 - }
 - return sb.toString().trim();
 - }
 - public boolean close() throws Exception {
 - this.connection = null;
 - return true;
 - }
 - public boolean post(String url) throws Exception {
 - hp = new HttpPost(url);
 - return true;
 - }
 - public boolean put(String url) throws Exception {
 - hpt = new HttpPut(url);
 - return true;
 - }
 - public boolean get(String url) throws Exception {
 - hg = new HttpGet(url);
 - return true;
 - }
 - public void setRequestProperty(String key, String value) throws Exception {
 - if (hp != null)
 - hp.setHeader(key, value);
 - else if (hpt != null)
 - hpt.setHeader(key, value);
 - else if (hg != null)
 - hg.setHeader(key, value);
 - }
 - public int getResponseCode() throws Exception {
 - if (hg != null) {
 - if (connection == null)
 - connection = getThreadSafeHttpClient();
 - response = connection.execute(hg);
 - }
 - return response.getStatusLine().getStatusCode();
 - }
 - public void setRequest(String req, String mime, String enc)
 - throws Exception {
 - StringEntity entity = new StringEntity(req, enc);
 - entity.setContentType(mime);
 - if (hp != null) {
 - hp.setEntity(entity);
 - if (connection == null)
 - connection = getThreadSafeHttpClient();
 - response = connection.execute(hp);
 - }
 - if (hpt != null) {
 - hpt.setEntity(entity);
 - if (connection == null)
 - connection = getThreadSafeHttpClient();
 - response = connection.execute(hpt);
 - }
 - }
 - public void setRequest(byte[] req, String mime) throws Exception {
 - ByteArrayEntity bae = new ByteArrayEntity(req);
 - if (hp != null) {
 - bae.setContentType(mime);
 - hp.setEntity(bae);
 - if (connection == null)
 - connection = getThreadSafeHttpClient();
 - response = connection.execute(hp);
 - }
 - if (hpt != null) {
 - //bae.setContentType(mime);
 - hpt.setEntity(bae);
 - if (connection == null)
 - connection = getThreadSafeHttpClient();
 - response = connection.execute(hpt);
 - }
 - }
 - public InputStream openInputStream() throws Exception {
 - InputStream inputstream = null;
 - if (this.connection != null) {
 - inputstream = response.getEntity().getContent();
 - }
 - return inputstream;
 - }
 - public DataInputStream openDataInputStream() throws Exception {
 - DataInputStream inputstream = null;
 - if (this.connection != null) {
 - inputstream = new DataInputStream(this.openInputStream());
 - }
 - return inputstream;
 - }
 - }
 
            
 
 знает толк
 
 
 
