下面列出了怎么用org.apache.commons.httpclient.HttpVersion的API类实例代码及写法,或者点击链接到github查看源代码。
@Test(groups = "wso2.esb", description = "Sending HTTP1.0 message")
public void sendingHTTP10Message() throws Exception {
PostMethod post = new PostMethod(getProxyServiceURLHttp("StockQuoteProxyTestHTTPVersion"));
RequestEntity entity = new StringRequestEntity(getPayload(), "text/xml", "UTF-8");
post.setRequestEntity(entity);
post.setRequestHeader("SOAPAction", "urn:getQuote");
HttpMethodParams params = new HttpMethodParams();
params.setVersion(HttpVersion.HTTP_1_0);
post.setParams(params);
HttpClient httpClient = new HttpClient();
String httpVersion = "";
try {
httpClient.executeMethod(post);
post.getResponseBodyAsString();
httpVersion = post.getStatusLine().getHttpVersion();
} finally {
post.releaseConnection();
}
Assert.assertEquals(httpVersion, HttpVersion.HTTP_1_0.toString(), "Http version mismatched");
}
@Test(groups = "wso2.esb", description = "Sending HTTP1.1 message")
public void sendingHTTP11Message() throws Exception {
PostMethod post = new PostMethod(getProxyServiceURLHttp("StockQuoteProxyTestHTTPVersion"));
RequestEntity entity = new StringRequestEntity(getPayload(), "text/xml", "UTF-8");
post.setRequestEntity(entity);
post.setRequestHeader("SOAPAction", "urn:getQuote");
HttpMethodParams params = new HttpMethodParams();
params.setVersion(HttpVersion.HTTP_1_1);
post.setParams(params);
HttpClient httpClient = new HttpClient();
String httpVersion = "";
try {
httpClient.executeMethod(post);
post.getResponseBodyAsString();
httpVersion = post.getStatusLine().getHttpVersion();
} finally {
post.releaseConnection();
}
Assert.assertEquals(httpVersion, HttpVersion.HTTP_1_1.toString(), "Http version mismatched");
}
@Test(groups = "wso2.esb", description = "Sending HTTP1.0 message")
public void sendingHTTP10Message() throws Exception {
PostMethod post = new PostMethod(getProxyServiceURLHttp("StockQuoteProxyTestHTTPVersion"));
RequestEntity entity = new StringRequestEntity(getPayload(), "text/xml", "UTF-8");
post.setRequestEntity(entity);
post.setRequestHeader("SOAPAction", "urn:getQuote");
HttpMethodParams params = new HttpMethodParams();
params.setVersion(HttpVersion.HTTP_1_0);
post.setParams(params);
HttpClient httpClient = new HttpClient();
String httpVersion = "";
try {
httpClient.executeMethod(post);
post.getResponseBodyAsString();
httpVersion = post.getStatusLine().getHttpVersion();
} finally {
post.releaseConnection();
}
Assert.assertEquals(httpVersion, HttpVersion.HTTP_1_0.toString(), "Http version mismatched");
}
@Test(groups = "wso2.esb", description = "Sending HTTP1.1 message")
public void sendingHTTP11Message() throws Exception {
PostMethod post = new PostMethod(getProxyServiceURLHttp("StockQuoteProxyTestHTTPVersion"));
RequestEntity entity = new StringRequestEntity(getPayload(), "text/xml", "UTF-8");
post.setRequestEntity(entity);
post.setRequestHeader("SOAPAction", "urn:getQuote");
HttpMethodParams params = new HttpMethodParams();
params.setVersion(HttpVersion.HTTP_1_1);
post.setParams(params);
HttpClient httpClient = new HttpClient();
String httpVersion = "";
try {
httpClient.executeMethod(post);
post.getResponseBodyAsString();
httpVersion = post.getStatusLine().getHttpVersion();
} finally {
post.releaseConnection();
}
Assert.assertEquals(httpVersion, HttpVersion.HTTP_1_1.toString(), "Http version mismatched");
}
/**
* Generates <tt>Content-Length</tt> or <tt>Transfer-Encoding: Chunked</tt>
* request header, as long as no <tt>Content-Length</tt> request header
* already exists.
*
* @param state current state of http requests
* @param conn the connection to use for I/O
*
* @throws IOException when errors occur reading or writing to/from the
* connection
* @throws HttpException when a recoverable error occurs
*/
protected void addContentLengthRequestHeader(HttpState state,
HttpConnection conn)
throws IOException, HttpException {
LOG.trace("enter EntityEnclosingMethod.addContentLengthRequestHeader("
+ "HttpState, HttpConnection)");
if ((getRequestHeader("content-length") == null)
&& (getRequestHeader("Transfer-Encoding") == null)) {
long len = getRequestContentLength();
if (len < 0) {
if (getEffectiveVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
addRequestHeader("Transfer-Encoding", "chunked");
} else {
throw new ProtocolException(getEffectiveVersion() +
" does not support chunk encoding");
}
} else {
addRequestHeader("Content-Length", String.valueOf(len));
}
}
}
/**
* Sets the <tt>Expect</tt> header if it has not already been set,
* in addition to the "standard" set of headers.
*
* @param state the {@link HttpState state} information associated with this method
* @param conn the {@link HttpConnection connection} used to execute
* this HTTP method
*
* @throws IOException if an I/O (transport) error occurs. Some transport exceptions
* can be recovered from.
* @throws HttpException if a protocol exception occurs. Usually protocol exceptions
* cannot be recovered from.
*/
protected void addRequestHeaders(HttpState state, HttpConnection conn)
throws IOException, HttpException {
LOG.trace("enter ExpectContinueMethod.addRequestHeaders(HttpState, HttpConnection)");
super.addRequestHeaders(state, conn);
// If the request is being retried, the header may already be present
boolean headerPresent = (getRequestHeader("Expect") != null);
// See if the expect header should be sent
// = HTTP/1.1 or higher
// = request body present
if (getParams().isParameterTrue(HttpMethodParams.USE_EXPECT_CONTINUE)
&& getEffectiveVersion().greaterEquals(HttpVersion.HTTP_1_1)
&& hasRequestContent())
{
if (!headerPresent) {
setRequestHeader("Expect", "100-continue");
}
} else {
if (headerPresent) {
removeRequestHeader("Expect");
}
}
}
/**
* Returns {@link HttpVersion HTTP protocol version} to be used by the
* {@link org.apache.commons.httpclient.HttpMethod HTTP methods} that
* this collection of parameters applies to.
*
* @return {@link HttpVersion HTTP protocol version}
*/
public HttpVersion getVersion() {
Object param = getParameter(PROTOCOL_VERSION);
if (param == null) {
return HttpVersion.HTTP_1_1;
}
return (HttpVersion)param;
}
private static HttpMethod createRequestMethod(
HttpRequestHeader header, HttpBody body, HttpMethodParams params) throws URIException {
HttpMethod httpMethod = new ZapGetMethod();
httpMethod.setURI(header.getURI());
httpMethod.setParams(params);
params.setVersion(HttpVersion.HTTP_1_1);
String msg = header.getHeadersAsString();
String[] split = Pattern.compile("\\r\\n", Pattern.MULTILINE).split(msg);
String token = null;
String name = null;
String value = null;
int pos = 0;
for (int i = 0; i < split.length; i++) {
token = split[i];
if (token.equals("")) {
continue;
}
if ((pos = token.indexOf(":")) < 0) {
return null;
}
name = token.substring(0, pos).trim();
value = token.substring(pos + 1).trim();
httpMethod.addRequestHeader(name, value);
}
if (body != null && body.length() > 0 && (httpMethod instanceof EntityEnclosingMethod)) {
EntityEnclosingMethod post = (EntityEnclosingMethod) httpMethod;
post.setRequestEntity(new ByteArrayRequestEntity(body.getBytes()));
}
httpMethod.setFollowRedirects(false);
return httpMethod;
}
/**
* Test that the deferredTask handler is installed.
*/
public void testDeferredTask() throws Exception {
// Replace the API proxy delegate so we can fake API responses.
FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate();
ApiProxy.setDelegate(fakeApiProxy);
// Add a api response so the task queue api is happy.
TaskQueueBulkAddResponse taskAddResponse = new TaskQueueBulkAddResponse();
TaskResult taskResult = taskAddResponse.addTaskResult();
taskResult.setResult(ErrorCode.OK.getValue());
taskResult.setChosenTaskName("abc");
fakeApiProxy.addApiResponse(taskAddResponse);
// Issue a deferredTaskRequest with payload.
String testData = "0987654321acbdefghijklmn";
String[] lines = fetchUrl(createUrl("/testTaskQueue?deferredTask=1&deferredData=" + testData));
TaskQueueBulkAddRequest request = new TaskQueueBulkAddRequest();
request.parseFrom(fakeApiProxy.getLastRequest().requestData);
assertEquals(1, request.addRequestSize());
TaskQueueAddRequest addRequest = request.getAddRequest(0);
assertEquals(TaskQueueAddRequest.RequestMethod.POST.getValue(), addRequest.getMethod());
// Pull out the request and fire it at the app.
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
PostMethod post = new PostMethod(createUrl(addRequest.getUrl()).toString());
post.getParams().setVersion(HttpVersion.HTTP_1_0);
// Add the required Task queue header, plus any headers from the request.
post.addRequestHeader("X-AppEngine-QueueName", "1");
for (TaskQueueAddRequest.Header header : addRequest.headers()) {
post.addRequestHeader(header.getKey(), header.getValue());
}
post.setRequestEntity(new ByteArrayRequestEntity(addRequest.getBodyAsBytes()));
int httpCode = httpClient.executeMethod(post);
assertEquals(HttpURLConnection.HTTP_OK, httpCode);
// Verify that the task was handled and that the payload is correct.
lines = fetchUrl(createUrl("/testTaskQueue?getLastPost=1"));
assertEquals("deferredData:" + testData, lines[lines.length - 1]);
}
/**
* Assigns the {@link HttpVersion HTTP protocol version} to be used by the
* {@link org.apache.commons.httpclient.HttpMethod HTTP methods} that
* this collection of parameters applies to.
*
* @param version the {@link HttpVersion HTTP protocol version}
*/
public void setVersion(HttpVersion version) {
setParameter(PROTOCOL_VERSION, version);
}