Example usage for org.apache.http.client.methods HttpUriRequest setHeader

List of usage examples for org.apache.http.client.methods HttpUriRequest setHeader

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpUriRequest setHeader.

Prototype

void setHeader(String str, String str2);

Source Link

Usage

From source file:com.foundationdb.http.CsrfProtectionITBase.java

@Test
public void postBlockedWithBadHost() throws Exception {
    HttpUriRequest request = new HttpPost(defaultURI());
    request.setHeader("Referer", "https://coolest.site.edu.fake.com:4320");

    response = client.execute(request);/*from  ww w  .ja v a 2 s  .  c  om*/
    assertEquals("status", HttpStatus.SC_FORBIDDEN, response.getStatusLine().getStatusCode());
    assertThat("reason", response.getStatusLine().getReasonPhrase(), containsString("Referer"));
}

From source file:com.foundationdb.http.CsrfProtectionITBase.java

@Test
public void getBlockedHasJsonResponse() throws Exception {
    HttpUriRequest request = new HttpGet(defaultURI());
    request.setHeader("Referer", "https://coolest.site.edu.fake.com:4320");

    response = client.execute(request);/*from   ww  w . j a  v a 2s. c  om*/
    assertEquals("status", HttpStatus.SC_FORBIDDEN, response.getStatusLine().getStatusCode());
    assertThat("reason", response.getStatusLine().getReasonPhrase(), containsString("Referer"));
    assertThat("body", EntityUtils.toString(response.getEntity()), containsString("Referer"));
}

From source file:com.foundationdb.http.CsrfProtectionITBase.java

@Test
public void getAllowedWithBlankReferer() throws Exception {
    // Since GET requests don't have side effects, the cross-origin header will prevent
    // third-party javascript from viewing the result, meaning that we can allow this through.
    HttpUriRequest request = new HttpGet(defaultURI());
    request.setHeader("Referer", "");

    response = client.execute(request);/*from ww w  .  ja v a  2 s  .c om*/
    assertEquals("status", HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
}

From source file:com.foundationdb.http.CsrfProtectionITBase.java

@Test
public void getBlockedWithBadHost() throws Exception {
    // Although we let blank & empty referers through for get requests, there is no benefit to
    // letting incorrect referers through, so those are always blocked.
    HttpUriRequest request = new HttpGet(defaultURI());
    request.setHeader("Referer", "https://coolest.site.edu.fake.com:4320");

    response = client.execute(request);// ww  w. j a v a 2  s .  c om
    assertEquals("status", HttpStatus.SC_FORBIDDEN, response.getStatusLine().getStatusCode());
    assertThat("reason", response.getStatusLine().getReasonPhrase(), containsString("Referer"));
}

From source file:com.skydragon.gplay.loopj.android.http.RangeFileAsyncHttpResponseHandler.java

public void updateRequestHeaders(HttpUriRequest uriRequest) {
    if (file.exists() && file.canWrite()) {
        current = file.length();/*w w w.  j  a  v a  2 s  .c  o  m*/
    }

    if (current > 0) {
        append = true;
        uriRequest.setHeader("Range", "bytes=" + current + "-");
    }
}

From source file:com.foundationdb.http.CsrfProtectionITBase.java

@Test
public void deleteAllowed() throws Exception {
    HttpUriRequest request = new HttpDelete(defaultURI("/1"));
    request.setHeader("Referer", "http://somewhere.com");

    response = client.execute(request);/*w  w w  .  ja  va 2 s.  c om*/
    assertEquals("status", HttpStatus.SC_NO_CONTENT, response.getStatusLine().getStatusCode());
}

From source file:org.quizpoll.net.UrlShortenerHelper.java

@Override
public HttpUriRequest createRequest() {
    try {/*from ww w. j ava2 s .  c o m*/
        Uri url = Uri.parse(URL_SHORTENER_URL).buildUpon().appendQueryParameter("key", URL_SHORTENER_API_KEY)
                .build();
        HttpUriRequest request = new HttpPost(url.toString());
        String requestString = "{\"longUrl\": \"" + (String) requestData + "\"}";
        ((HttpPost) request).setEntity(new StringEntity(requestString));
        request.setHeader("Content-Type", "application/json");
        return request;
    } catch (UnsupportedEncodingException e) {
        return null;
    }
}

From source file:com.betfair.cougar.baseline.security.BaselineClientIdentityTokenResolver.java

@Override
public void rewrite(List<IdentityToken> credentials, HttpUriRequest output) {
    if (credentials != null) {
        for (IdentityToken token : credentials) {
            try {
                String tokenName = SimpleIdentityTokenName.valueOf(token.getName()).name();
                output.setHeader(TOKEN_PREFIX + tokenName, token.getValue());
            } catch (IllegalArgumentException e) {
                /*ignore*/ }
        }/*www  .j  av  a 2 s . c o m*/
    }
}

From source file:org.apache.vxquery.rest.ErrorResponseTest.java

private void runTest(URI uri, String accepts, int expectedStatusCode, String httpMethod) throws Exception {
    CloseableHttpClient httpClient = HttpClients.custom().setConnectionTimeToLive(20, TimeUnit.SECONDS).build();

    ErrorResponse errorResponse;/*from  w w w .j a  v a2 s  .c  o  m*/
    try {
        HttpUriRequest request = getRequest(uri, httpMethod);
        if (accepts != null) {
            request.setHeader(HttpHeaders.ACCEPT, accepts);
        }

        try (CloseableHttpResponse httpResponse = httpClient.execute(request)) {
            Assert.assertEquals(expectedStatusCode, httpResponse.getStatusLine().getStatusCode());
            if (accepts != null) {
                Assert.assertEquals(accepts, httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
            }

            HttpEntity entity = httpResponse.getEntity();
            Assert.assertNotNull(entity);

            String response = RestUtils.readEntity(entity);
            errorResponse = RestUtils.mapEntity(response, ErrorResponse.class, accepts);
        }
    } finally {
        HttpClientUtils.closeQuietly(httpClient);
    }

    Assert.assertNotNull(errorResponse);
    Assert.assertNotNull(errorResponse.getError().getMessage());
    Assert.assertEquals(errorResponse.getError().getCode(), expectedStatusCode);
}

From source file:com.meltmedia.cadmium.blackbox.test.ApiRequest.java

private void setHeadersIfAny(HttpUriRequest request) {
    if (httpMethod != Method.GET && httpMethod != Method.DELETE && postContentType != null) {
        request.setHeader("Content-Type", postContentType);
    }//  w ww .j a va 2  s .  c  om
    if (headers != null && !headers.isEmpty()) {
        for (String header : headers.keySet()) {
            String value = headers.get(header);
            request.setHeader(header, value);
        }
    }
}