| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
package android.core; |
| 33 |
|
| 34 |
import java.io.IOException; |
| 35 |
|
| 36 |
import org.apache.http.ConnectionReuseStrategy; |
| 37 |
import org.apache.http.HttpClientConnection; |
| 38 |
import org.apache.http.HttpException; |
| 39 |
import org.apache.http.HttpHost; |
| 40 |
import org.apache.http.HttpRequest; |
| 41 |
import org.apache.http.HttpResponse; |
| 42 |
import org.apache.http.HttpVersion; |
| 43 |
import org.apache.http.impl.DefaultConnectionReuseStrategy; |
| 44 |
import org.apache.http.params.BasicHttpParams; |
| 45 |
import org.apache.http.params.CoreConnectionPNames; |
| 46 |
import org.apache.http.params.DefaultedHttpParams; |
| 47 |
import org.apache.http.params.HttpParams; |
| 48 |
import org.apache.http.params.CoreProtocolPNames; |
| 49 |
import org.apache.http.protocol.BasicHttpProcessor; |
| 50 |
import org.apache.http.protocol.HttpContext; |
| 51 |
import org.apache.http.protocol.BasicHttpContext; |
| 52 |
import org.apache.http.protocol.ExecutionContext; |
| 53 |
import org.apache.http.protocol.HttpRequestExecutor; |
| 54 |
import org.apache.http.protocol.RequestConnControl; |
| 55 |
import org.apache.http.protocol.RequestContent; |
| 56 |
import org.apache.http.protocol.RequestExpectContinue; |
| 57 |
import org.apache.http.protocol.RequestTargetHost; |
| 58 |
import org.apache.http.protocol.RequestUserAgent; |
| 59 |
|
| 60 |
public class TestHttpClient { |
| 61 |
|
| 62 |
private final HttpParams params; |
| 63 |
private final BasicHttpProcessor httpproc; |
| 64 |
private final HttpRequestExecutor httpexecutor; |
| 65 |
private final ConnectionReuseStrategy connStrategy; |
| 66 |
private final HttpContext context; |
| 67 |
|
| 68 |
public TestHttpClient() { |
| 69 |
super(); |
| 70 |
this.params = new BasicHttpParams(); |
| 71 |
this.params |
| 72 |
.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000) |
| 73 |
.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false) |
| 74 |
.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1) |
| 75 |
.setParameter(CoreProtocolPNames.USER_AGENT, "TEST-CLIENT/1.1"); |
| 76 |
|
| 77 |
this.httpproc = new BasicHttpProcessor(); |
| 78 |
|
| 79 |
this.httpproc.addInterceptor(new RequestContent()); |
| 80 |
this.httpproc.addInterceptor(new RequestTargetHost()); |
| 81 |
|
| 82 |
this.httpproc.addInterceptor(new RequestConnControl()); |
| 83 |
this.httpproc.addInterceptor(new RequestUserAgent()); |
| 84 |
this.httpproc.addInterceptor(new RequestExpectContinue()); |
| 85 |
|
| 86 |
this.httpexecutor = new HttpRequestExecutor(); |
| 87 |
this.connStrategy = new DefaultConnectionReuseStrategy(); |
| 88 |
this.context = new BasicHttpContext(null); |
| 89 |
} |
| 90 |
|
| 91 |
public HttpParams getParams() { |
| 92 |
return this.params; |
| 93 |
} |
| 94 |
|
| 95 |
public HttpResponse execute( |
| 96 |
final HttpRequest request, |
| 97 |
final HttpHost targetHost, |
| 98 |
final HttpClientConnection conn) throws HttpException, IOException { |
| 99 |
this.context.setAttribute(ExecutionContext.HTTP_REQUEST, request); |
| 100 |
this.context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, targetHost); |
| 101 |
this.context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); |
| 102 |
request.setParams( |
| 103 |
new DefaultedHttpParams(request.getParams(), this.params)); |
| 104 |
this.httpexecutor.preProcess(request, this.httpproc, this.context); |
| 105 |
HttpResponse response = this.httpexecutor.execute(request, conn, this.context); |
| 106 |
response.setParams( |
| 107 |
new DefaultedHttpParams(response.getParams(), this.params)); |
| 108 |
this.httpexecutor.postProcess(response, this.httpproc, this.context); |
| 109 |
return response; |
| 110 |
} |
| 111 |
|
| 112 |
public boolean keepAlive(final HttpResponse response) { |
| 113 |
return this.connStrategy.keepAlive(response, this.context); |
| 114 |
} |
| 115 |
|
| 116 |
} |