當前位置: 首頁>>代碼示例>>Java>>正文


Java BasicHttpEntityEnclosingRequest.setHeader方法代碼示例

本文整理匯總了Java中org.apache.http.message.BasicHttpEntityEnclosingRequest.setHeader方法的典型用法代碼示例。如果您正苦於以下問題:Java BasicHttpEntityEnclosingRequest.setHeader方法的具體用法?Java BasicHttpEntityEnclosingRequest.setHeader怎麽用?Java BasicHttpEntityEnclosingRequest.setHeader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.http.message.BasicHttpEntityEnclosingRequest的用法示例。


在下文中一共展示了BasicHttpEntityEnclosingRequest.setHeader方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testResponsesToPOSTWithoutCacheControlOrExpiresAreNotCached

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
@Test
public void testResponsesToPOSTWithoutCacheControlOrExpiresAreNotCached() throws Exception {
    emptyMockCacheExpectsNoPuts();

    final BasicHttpEntityEnclosingRequest post = new BasicHttpEntityEnclosingRequest("POST", "/",
            HttpVersion.HTTP_1_1);
    post.setHeader("Content-Length", "128");
    post.setEntity(HttpTestUtils.makeBody(128));

    originResponse.removeHeaders("Cache-Control");
    originResponse.removeHeaders("Expires");

    EasyMock.expect(
            mockBackend.execute(
                    EasyMock.isA(HttpRoute.class),
                    EasyMock.isA(HttpRequestWrapper.class),
                    EasyMock.isA(HttpClientContext.class),
                    EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse);

    replayMocks();

    impl.execute(route, HttpRequestWrapper.wrap(post), context, null);

    verifyMocks();
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:26,代碼來源:TestProtocolRequirements.java

示例2: testDoesNotModifyHeaderOnRequest

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
private void testDoesNotModifyHeaderOnRequest(final String header, final String value) throws Exception {
    final BasicHttpEntityEnclosingRequest req =
        new BasicHttpEntityEnclosingRequest("POST","/",HttpVersion.HTTP_1_1);
    req.setEntity(HttpTestUtils.makeBody(128));
    req.setHeader("Content-Length","128");
    req.setHeader(header,value);

    final Capture<HttpRequestWrapper> cap = new Capture<HttpRequestWrapper>();

    EasyMock.expect(
            mockBackend.execute(
                    EasyMock.eq(route),
                    EasyMock.capture(cap),
                    EasyMock.isA(HttpClientContext.class),
                    EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse);

    replayMocks();
    impl.execute(route, HttpRequestWrapper.wrap(req), context, null);
    verifyMocks();

    final HttpRequest captured = cap.getValue();
    Assert.assertEquals(value, captured.getFirstHeader(header).getValue());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:24,代碼來源:TestProtocolRequirements.java

示例3: testDoesNotAddHeaderToRequestIfNotPresent

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
private void testDoesNotAddHeaderToRequestIfNotPresent(final String header) throws Exception {
    final BasicHttpEntityEnclosingRequest req =
        new BasicHttpEntityEnclosingRequest("POST","/",HttpVersion.HTTP_1_1);
    req.setEntity(HttpTestUtils.makeBody(128));
    req.setHeader("Content-Length","128");
    req.removeHeaders(header);

    final Capture<HttpRequestWrapper> cap = new Capture<HttpRequestWrapper>();

    EasyMock.expect(
            mockBackend.execute(
                    EasyMock.eq(route),
                    EasyMock.capture(cap),
                    EasyMock.isA(HttpClientContext.class),
                    EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse);

    replayMocks();
    impl.execute(route, HttpRequestWrapper.wrap(req), context, null);
    verifyMocks();

    final HttpRequest captured = cap.getValue();
    Assert.assertNull(captured.getFirstHeader(header));
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:24,代碼來源:TestProtocolRequirements.java

示例4: testJsonBody

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
@Test
public void testJsonBody() throws Exception {
    MyController controller = new MyController();

    ReflectionControllerRequestHandler handler = new ControllerBuilder(new GsonBuilder().create())
            .addController(controller)
            .withPathPrefix("/")
            .create();


    HttpCoreContext context = HttpCoreContext.create();
    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("PUT", "/stuff/putthis");
    request.setHeader("Content-Type", "application/json");
    request.setEntity(new StringEntity("{ \"name\": \"paul\" }"));


    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, ""));

    handler.handle(request, response, context);

    assertTrue(response.getStatusLine().getStatusCode() == 200);
    String resultBody = EntityUtils.toString(response.getEntity());
    assertTrue("itworked".equals(resultBody));
}
 
開發者ID:dinocore1,項目名稱:MiniWeb,代碼行數:25,代碼來源:ReflectionControllerTest.java

示例5: testRequestsExpecting100ContinueBehaviorShouldSetExpectHeader

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
@Test
public void testRequestsExpecting100ContinueBehaviorShouldSetExpectHeader() throws Exception {
    final BasicHttpEntityEnclosingRequest post = new BasicHttpEntityEnclosingRequest(
            "POST", "/", HttpVersion.HTTP_1_1);
    post.setHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
    post.setHeader("Content-Length", "128");
    post.setEntity(new BasicHttpEntity());

    final Capture<HttpRequestWrapper> reqCap = new Capture<HttpRequestWrapper>();

    EasyMock.expect(
            mockBackend.execute(
                    EasyMock.eq(route),
                    EasyMock.capture(reqCap),
                    EasyMock.isA(HttpClientContext.class),
                    EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse);

    replayMocks();

    impl.execute(route, HttpRequestWrapper.wrap(post), context, null);

    verifyMocks();

    final HttpRequestWrapper forwarded = reqCap.getValue();
    boolean foundExpect = false;
    for (final Header h : forwarded.getHeaders("Expect")) {
        for (final HeaderElement elt : h.getElements()) {
            if ("100-continue".equalsIgnoreCase(elt.getName())) {
                foundExpect = true;
                break;
            }
        }
    }
    Assert.assertTrue(foundExpect);
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:36,代碼來源:TestProtocolRequirements.java

示例6: testRequestsNotExpecting100ContinueBehaviorShouldNotSetExpectContinueHeader

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
@Test
public void testRequestsNotExpecting100ContinueBehaviorShouldNotSetExpectContinueHeader()
        throws Exception {
    final BasicHttpEntityEnclosingRequest post = new BasicHttpEntityEnclosingRequest(
            "POST", "/", HttpVersion.HTTP_1_1);
    post.setHeader("Content-Length", "128");
    post.setEntity(new BasicHttpEntity());

    final Capture<HttpRequestWrapper> reqCap = new Capture<HttpRequestWrapper>();

    EasyMock.expect(
            mockBackend.execute(
                    EasyMock.eq(route),
                    EasyMock.capture(reqCap),
                    EasyMock.isA(HttpClientContext.class),
                    EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse);

    replayMocks();

    impl.execute(route, HttpRequestWrapper.wrap(post), context, null);

    verifyMocks();

    final HttpRequestWrapper forwarded = reqCap.getValue();
    boolean foundExpect = false;
    for (final Header h : forwarded.getHeaders("Expect")) {
        for (final HeaderElement elt : h.getElements()) {
            if ("100-continue".equalsIgnoreCase(elt.getName())) {
                foundExpect = true;
                break;
            }
        }
    }
    Assert.assertFalse(foundExpect);
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:36,代碼來源:TestProtocolRequirements.java

示例7: test100ContinueResponsesAreNotForwardedTo1_0ClientsWhoDidNotAskForThem

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
@Test
public void test100ContinueResponsesAreNotForwardedTo1_0ClientsWhoDidNotAskForThem()
        throws Exception {

    final BasicHttpEntityEnclosingRequest post = new BasicHttpEntityEnclosingRequest("POST", "/",
            new ProtocolVersion("HTTP", 1, 0));
    post.setEntity(body);
    post.setHeader("Content-Length", "128");

    originResponse = Proxies.enhanceResponse(
            new BasicHttpResponse(HttpVersion.HTTP_1_1, 100, "Continue"));
    EasyMock.expect(
            mockBackend.execute(
                    EasyMock.eq(route),
                    EasyMock.isA(HttpRequestWrapper.class),
                    EasyMock.isA(HttpClientContext.class),
                    EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse);
    replayMocks();

    try {
        // if a 100 response gets up to us from the HttpClient
        // backend, we can't really handle it at that point
        impl.execute(route, HttpRequestWrapper.wrap(post), context, null);
        Assert.fail("should have thrown an exception");
    } catch (final ClientProtocolException expected) {
    }

    verifyMocks();
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:30,代碼來源:TestProtocolRequirements.java

示例8: testForwardedTRACERequestsDoNotIncludeAnEntity

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
@Test
public void testForwardedTRACERequestsDoNotIncludeAnEntity() throws Exception {
    final BasicHttpEntityEnclosingRequest trace = new BasicHttpEntityEnclosingRequest("TRACE", "/",
            HttpVersion.HTTP_1_1);
    trace.setEntity(HttpTestUtils.makeBody(entityLength));
    trace.setHeader("Content-Length", Integer.toString(entityLength));

    final Capture<HttpRequestWrapper> reqCap = new Capture<HttpRequestWrapper>();

    EasyMock.expect(
            mockBackend.execute(
                    EasyMock.eq(route),
                    EasyMock.capture(reqCap),
                    EasyMock.isA(HttpClientContext.class),
                    EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse);

    replayMocks();
    impl.execute(route, HttpRequestWrapper.wrap(trace), context, null);
    verifyMocks();

    final HttpRequest forwarded = reqCap.getValue();
    if (forwarded instanceof HttpEntityEnclosingRequest) {
        final HttpEntityEnclosingRequest bodyReq = (HttpEntityEnclosingRequest) forwarded;
        Assert.assertTrue(bodyReq.getEntity() == null
                || bodyReq.getEntity().getContentLength() == 0);
    } else {
        // request didn't enclose an entity
    }
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:30,代碼來源:TestProtocolRequirements.java

示例9: testOPTIONSRequestsWithBodiesAndNoContentTypeHaveOneSupplied

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
@Test
public void testOPTIONSRequestsWithBodiesAndNoContentTypeHaveOneSupplied() throws Exception {
    final BasicHttpEntityEnclosingRequest options = new BasicHttpEntityEnclosingRequest("OPTIONS",
            "/", HTTP_1_1);
    options.setEntity(body);
    options.setHeader("Content-Length", "1");

    final Capture<HttpRequestWrapper> reqCap = new Capture<HttpRequestWrapper>();
    EasyMock.expect(
            mockBackend.execute(
                    EasyMock.eq(route),
                    EasyMock.capture(reqCap),
                    EasyMock.isA(HttpClientContext.class),
                    EasyMock.<HttpExecutionAware>isNull())).andReturn(originResponse);
    replayMocks();

    impl.execute(route, HttpRequestWrapper.wrap(options), context, null);

    verifyMocks();

    final HttpRequest forwarded = reqCap.getValue();
    Assert.assertTrue(forwarded instanceof HttpEntityEnclosingRequest);
    final HttpEntityEnclosingRequest reqWithBody = (HttpEntityEnclosingRequest) forwarded;
    final HttpEntity reqBody = reqWithBody.getEntity();
    Assert.assertNotNull(reqBody);
    Assert.assertNotNull(reqBody.getContentType());
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:28,代碼來源:TestProtocolDeviations.java

示例10: execute_doauth

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
private HttpResponse execute_doauth(String method, String uri, StringEntity entity,
            int retry_count) throws IOException {

    checkConnection();

    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(method, uri);
    if (entity != null) {
        request.setEntity(entity);
        s_logger.info(">> Request: " + method + ", " + request.getRequestLine().getUri() +
                ", " + EntityUtils.toString(entity));
    } else {
        s_logger.info(">> Request: " + method + ", " + request.getRequestLine().getUri());
    }
    HttpResponse response  = null;
    request.setParams(_params);
    if (_authtoken != null) {
        request.setHeader("X-AUTH-TOKEN", _authtoken);
    }
    try {
        _httpexecutor.preProcess(request, _httpproc, _httpcontext);
        response = _httpexecutor.execute(request, _connection, _httpcontext);
        response.setParams(_params);
        _httpexecutor.postProcess(response, _httpproc, _httpcontext);
    } catch (Exception e) {
        if (retry_count == 0) {
            s_logger.error("<< Received exception from the Api server, max retries exhausted: " + e);
            s_logger.error(Throwables.getStackTraceAsString(e));
            return null;
        }
        s_logger.info("<< Api server connection timed out, retrying " + retry_count + " more times");
        return execute_doauth(method, uri, entity, --retry_count);
    }

    if (response == null) {
         if (retry_count == 0) {
             s_logger.error("<< Received null response from the Api server, max retries exhausted");
             return null;
         }
         s_logger.error("<< Received null response from the Api server, retrying "
                        + retry_count + " more times");
         return execute_doauth(method, uri, entity, --retry_count);
    }

    s_logger.info("<< Response Status: " + response.getStatusLine());
    if ((response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED
            && retry_count > 0)) {
        if (authenticate()) {
            getResponseData(response);
            checkResponseKeepAliveStatus(response);
            s_logger.error("<< Received \"unauthorized response from the Api server, retrying "
                    + retry_count + " more times after authentication");
            return execute_doauth(method, uri, entity, --retry_count);
        }
    }

    return response;
}
 
開發者ID:Juniper,項目名稱:contrail-java-api,代碼行數:58,代碼來源:ApiConnectorImpl.java

示例11: testInheritController

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
@Test
public void testInheritController() throws Exception {

    MyInheritController controller = new MyInheritController();

    ReflectionControllerRequestHandler handler = new ControllerBuilder(new GsonBuilder().create())
            .addController(controller)
            .withPathPrefix("/")
            .create();


    HttpCoreContext context = HttpCoreContext.create();
    BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("PUT", "/stuff/putthis");
    request.setHeader("Content-Type", "application/json");
    request.setEntity(new StringEntity("{ \"name\": \"paul\" }"));


    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, ""));

    handler.handle(request, response, context);

    assertTrue(response.getStatusLine().getStatusCode() == 200);
    String resultBody = EntityUtils.toString(response.getEntity());
    assertTrue("itworked".equals(resultBody));
}
 
開發者ID:dinocore1,項目名稱:MiniWeb,代碼行數:26,代碼來源:ReflectionControllerTest.java

示例12: getCoapRequestPayloadTest

import org.apache.http.message.BasicHttpEntityEnclosingRequest; //導入方法依賴的package包/類
@Test
public final void getCoapRequestPayloadTest() throws TranslationException {
    String coapServerUri = "coap://coapServer:5684/helloWorld";

    for (String httpMethod : COAP_METHODS) {
        // create the http request
        RequestLine requestLine = new BasicRequestLine(httpMethod, "/" + PROXY_RESOURCE + "/" + coapServerUri, HttpVersion.HTTP_1_1);
        BasicHttpEntityEnclosingRequest httpRequest = new BasicHttpEntityEnclosingRequest(requestLine);

        // create the entity
        String contentString = "aaa";
        HttpEntity httpEntity = new ByteArrayEntity(contentString.getBytes(Charset.forName("ISO_8859_1")), ContentType.TEXT_PLAIN);
        httpRequest.setEntity(httpEntity);

        // set the content-type
        httpRequest.setHeader("content-type", "text/plain;  charset=iso-8859-1");

        // create the header
        String headerName = "if-match";
        String headerValue = "\"737060cd8c284d8af7ad3082f209582d\"";
        Header header = new BasicHeader(headerName, headerValue);
        httpRequest.addHeader(header);

        // translate the request
        Request coapRequest = HttpTranslator.getCoapRequest(httpRequest, PROXY_RESOURCE, true);
        assertNotNull(httpRequest);

        // check the method translation
        int coapMethod = Integer.parseInt(HttpTranslator.HTTP_TRANSLATION_PROPERTIES.getProperty("http.request.method." + httpMethod));
        assertTrue(coapRequest.getCode() == coapMethod);

        // check the proxy-uri
        String proxyUri = coapRequest.getFirstOption(OptionNumberRegistry.PROXY_URI).getStringValue();
        assertEquals(proxyUri, coapServerUri);

        // check the absence of the uri-* options
        assertNull(coapRequest.getFirstOption(OptionNumberRegistry.URI_PATH));
        assertNull(coapRequest.getFirstOption(OptionNumberRegistry.URI_HOST));
        assertNull(coapRequest.getFirstOption(OptionNumberRegistry.URI_QUERY));
        assertNull(coapRequest.getFirstOption(OptionNumberRegistry.URI_PORT));

        // check the payload
        assertNotNull(coapRequest.getPayload());
        assertArrayEquals(contentString.getBytes(Charset.forName("UTF-8")), coapRequest.getPayload());

        // check the option
        assertFalse(coapRequest.getOptions().isEmpty());
        int optionNumber = Integer.parseInt(HttpTranslator.HTTP_TRANSLATION_PROPERTIES.getProperty("http.message.header." + headerName));
        assertEquals(coapRequest.getFirstOption(optionNumber).getStringValue(), headerValue);

        // check the content-type
        assertEquals(coapRequest.getContentType(), MediaTypeRegistry.TEXT_PLAIN);
    }
}
 
開發者ID:Orange-OpenSource,項目名稱:holico,代碼行數:55,代碼來源:HttpTranslatorTest.java


注:本文中的org.apache.http.message.BasicHttpEntityEnclosingRequest.setHeader方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。