本文整理匯總了Java中org.apache.http.message.BasicHttpRequest類的典型用法代碼示例。如果您正苦於以下問題:Java BasicHttpRequest類的具體用法?Java BasicHttpRequest怎麽用?Java BasicHttpRequest使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BasicHttpRequest類屬於org.apache.http.message包,在下文中一共展示了BasicHttpRequest類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: newHttpRequest
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
public HttpRequest newHttpRequest(final RequestLine requestline)
throws MethodNotSupportedException {
if (requestline == null) {
throw new IllegalArgumentException("Request line may not be null");
}
String method = requestline.getMethod();
if (isOneOf(RFC2616_COMMON_METHODS, method)) {
return new BasicHttpRequest(requestline);
} else if (isOneOf(RFC2616_ENTITY_ENC_METHODS, method)) {
return new BasicHttpEntityEnclosingRequest(requestline);
} else if (isOneOf(RFC2616_SPECIAL_METHODS, method)) {
return new BasicHttpRequest(requestline);
} else {
throw new MethodNotSupportedException(method + " method not supported");
}
}
示例2: logsRequestAndResponseFields
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void logsRequestAndResponseFields() {
HttpContext context = new BasicHttpContext();
context.setAttribute(HTTP_TARGET_HOST, "http://www.google.com");
OutboundRequestLoggingInterceptor interceptor = new OutboundRequestLoggingInterceptor(new FakeClock(20));
interceptor.process(new BasicHttpRequest("GET", "/something"), context);
interceptor.process(new BasicHttpResponse(new BasicStatusLine(ANY_PROTOCOL, 200, "any")), context);
Map<String, Object> fields = new ConcurrentHashMap<>();
fields.put("requestMethod", "GET");
fields.put("requestURI", "http://www.google.com/something");
testAppender.assertEvent(0, INFO, "Outbound request start", appendEntries(fields));
fields.put("responseTime", 20L);
fields.put("responseCode", 200);
testAppender.assertEvent(1, INFO, "Outbound request finish", appendEntries(fields));
}
示例3: allowEmptyConstructorToBuildDefaultClock
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void allowEmptyConstructorToBuildDefaultClock() {
testAppender.clearEvents();
HttpContext context = new BasicHttpContext();
context.setAttribute(HTTP_TARGET_HOST, "http://www.google.com");
OutboundRequestLoggingInterceptor interceptor = new OutboundRequestLoggingInterceptor();
interceptor.process(new BasicHttpRequest("GET", "/something"), context);
interceptor.process(new BasicHttpResponse(new BasicStatusLine(ANY_PROTOCOL, 200, "any")), context);
assertThat(testAppender.getEvents()).extracting("message")
.contains("Outbound request start", Index.atIndex(0))
.contains("Outbound request finish", Index.atIndex(1));
}
示例4: testDigestAuthenticationWithQueryStringInDigestURI
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testDigestAuthenticationWithQueryStringInDigestURI() throws Exception {
final String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
final Header authChallenge = new BasicHeader(AUTH.WWW_AUTH, challenge);
final HttpRequest request = new BasicHttpRequest("Simple", "/?param=value");
final Credentials cred = new UsernamePasswordCredentials("username","password");
final HttpContext context = new BasicHttpContext();
final DigestScheme authscheme = new DigestScheme();
authscheme.processChallenge(authChallenge);
final Header authResponse = authscheme.authenticate(cred, request, context);
final Map<String, String> table = parseAuthResponse(authResponse);
Assert.assertEquals("username", table.get("username"));
Assert.assertEquals("realm1", table.get("realm"));
Assert.assertEquals("/?param=value", table.get("uri"));
Assert.assertEquals("f2a3f18799759d4f1a1c068b92b573cb", table.get("nonce"));
Assert.assertEquals("a847f58f5fef0bc087bcb9c3eb30e042", table.get("response"));
}
示例5: submitRequest
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
public HttpRequest submitRequest(final HttpContext context) {
HttpHost targetHost = (HttpHost) context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
Object flag = context.getAttribute(REQUEST_SENT);
if (flag == null) {
// Stick some object into the context
context.setAttribute(REQUEST_SENT, Boolean.TRUE);
System.out.println("--------------");
System.out.println("Sending request to " + targetHost);
System.out.println("--------------");
return new BasicHttpRequest("GET", "/");
} else {
// No new request to submit
return null;
}
}
示例6: testAuthCacheEmpty
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testAuthCacheEmpty() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
final AuthCache authCache = new BasicAuthCache();
context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
final HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);
Assert.assertNull(this.targetState.getAuthScheme());
Assert.assertNull(this.targetState.getCredentials());
Assert.assertNull(this.proxyState.getAuthScheme());
Assert.assertNull(this.proxyState.getCredentials());
}
示例7: testAddCookiesUsingExplicitCookieSpec
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testAddCookiesUsingExplicitCookieSpec() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final RequestConfig config = RequestConfig.custom()
.setCookieSpec(CookieSpecs.NETSCAPE).build();
final HttpRoute route = new HttpRoute(this.target, null, false);
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
final HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
final CookieSpec cookieSpec = context.getCookieSpec();
Assert.assertTrue(cookieSpec instanceof NetscapeDraftSpec);
final Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(1, headers1.length);
Assert.assertEquals("name1=value1; name2=value2", headers1[0].getValue());
}
示例8: testExpiresHeaderMatchesDateIfAddedToCacheHit
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testExpiresHeaderMatchesDateIfAddedToCacheHit() throws Exception {
final HttpRequestWrapper req1 = HttpRequestWrapper.wrap(
new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(
new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
originResponse.setHeader("Cache-Control","max-age=3600");
originResponse.removeHeaders("Expires");
backendExpectsAnyRequest().andReturn(originResponse);
replayMocks();
impl.execute(route, req1, context, null);
final HttpResponse result = impl.execute(route, req2, context, null);
verifyMocks();
final Header expHdr = result.getFirstHeader("Expires");
if (expHdr != null) {
Assert.assertEquals(result.getFirstHeader("Date").getValue(),
expHdr.getValue());
}
}
示例9: testNoTargetHost
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testNoTargetHost() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpRoute route = new HttpRoute(this.target, null, false);
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, null);
context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
final HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
final Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(0, headers1.length);
final Header[] headers2 = request.getHeaders(SM.COOKIE2);
Assert.assertNotNull(headers2);
Assert.assertEquals(0, headers2.length);
}
示例10: testMayReturnStaleResponseIfClientExplicitlySpecifiesAcceptableMaxStale
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testMayReturnStaleResponseIfClientExplicitlySpecifiesAcceptableMaxStale()
throws Exception {
final HttpRequestWrapper req1 = requestToPopulateStaleCacheEntry();
final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(
new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
req2.setHeader("Cache-Control","max-stale=20");
backendExpectsAnyRequest().andThrow(new IOException()).times(0,1);
replayMocks();
impl.execute(route, req1, context, null);
final HttpResponse result = impl.execute(route, req2, context, null);
verifyMocks();
assertEquals(HttpStatus.SC_OK, result.getStatusLine().getStatusCode());
assertNotNull(result.getFirstHeader("Warning"));
}
示例11: testPreemptiveTargetAndProxyAuth
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testPreemptiveTargetAndProxyAuth() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
final AuthCache authCache = new BasicAuthCache();
authCache.put(this.target, this.authscheme1);
authCache.put(this.proxy, this.authscheme2);
context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
final HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);
Assert.assertNotNull(this.targetState.getAuthScheme());
Assert.assertSame(this.creds1, this.targetState.getCredentials());
Assert.assertNotNull(this.proxyState.getAuthScheme());
Assert.assertSame(this.creds2, this.proxyState.getCredentials());
}
示例12: testNoCacheCannotSatisfyASubsequentRequestWithoutRevalidationEvenWithContraryIndications
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testNoCacheCannotSatisfyASubsequentRequestWithoutRevalidationEvenWithContraryIndications()
throws Exception {
final HttpRequestWrapper req1 = HttpRequestWrapper.wrap(
new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
final HttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("ETag","\"etag\"");
resp1.setHeader("Cache-Control","no-cache,s-maxage=3600");
backendExpectsAnyRequestAndReturn(resp1);
final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(
new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
req2.setHeader("Cache-Control","max-stale=7200");
final HttpResponse resp2 = HttpTestUtils.make200Response();
// this MUST happen
backendExpectsAnyRequestAndReturn(resp2);
replayMocks();
impl.execute(route, req1, context, null);
impl.execute(route, req2, context, null);
verifyMocks();
}
示例13: testCacheControlPrivateIsNotCacheableBySharedCache
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testCacheControlPrivateIsNotCacheableBySharedCache()
throws Exception {
if (config.isSharedCache()) {
final HttpRequestWrapper req1 = HttpRequestWrapper.wrap(
new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
final HttpResponse resp1 = HttpTestUtils.make200Response();
resp1.setHeader("Cache-Control","private,max-age=3600");
backendExpectsAnyRequestAndReturn(resp1);
final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(
new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));
final HttpResponse resp2 = HttpTestUtils.make200Response();
// this backend request MUST happen
backendExpectsAnyRequestAndReturn(resp2);
replayMocks();
impl.execute(route, req1, context, null);
impl.execute(route, req2, context, null);
verifyMocks();
}
}
示例14: testCookiesForConnectRequest
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testCookiesForConnectRequest() throws Exception {
final HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com");
final HttpRoute route = new HttpRoute(this.target, null, false);
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
context.setAttribute(HttpClientContext.COOKIESPEC_REGISTRY, this.cookieSpecRegistry);
final HttpRequestInterceptor interceptor = new RequestAddCookies();
interceptor.process(request, context);
final Header[] headers1 = request.getHeaders(SM.COOKIE);
Assert.assertNotNull(headers1);
Assert.assertEquals(0, headers1.length);
final Header[] headers2 = request.getHeaders(SM.COOKIE2);
Assert.assertNotNull(headers2);
Assert.assertEquals(0, headers2.length);
}
示例15: testNoMatchingCredentials
import org.apache.http.message.BasicHttpRequest; //導入依賴的package包/類
@Test
public void testNoMatchingCredentials() throws Exception {
final HttpRequest request = new BasicHttpRequest("GET", "/");
this.credProvider.clear();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
final AuthCache authCache = new BasicAuthCache();
authCache.put(this.target, this.authscheme1);
authCache.put(this.proxy, this.authscheme2);
context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
final HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);
Assert.assertNull(this.targetState.getAuthScheme());
Assert.assertNull(this.targetState.getCredentials());
Assert.assertNull(this.proxyState.getAuthScheme());
Assert.assertNull(this.proxyState.getCredentials());
}