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


Java HttpOptions.setHeader方法代碼示例

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


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

示例1: testOptionsCorrectOrigin

import org.apache.http.client.methods.HttpOptions; //導入方法依賴的package包/類
@Test
public void testOptionsCorrectOrigin() throws Exception {
    String url = "http://localhost:8080";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpOptions httpOptions = new HttpOptions(url);
    httpOptions.setHeader("Origin", "http://localhost");
    httpOptions.setHeader("Access-Control-Request-Method", "POST");
    httpOptions.setHeader("Access-Control-Request-Headers", "X-Requested-With");

    try {
        CloseableHttpResponse response = client.execute(httpOptions);
        int statusCode = response.getStatusLine().getStatusCode();
        String body = IOUtils.toString(response.getEntity().getContent(), "utf8");
        Header[] headers = response.getAllHeaders();
        Header header = response.getFirstHeader("Access-Control-Allow-Origin");
        Assert.assertEquals(200, statusCode);
        if(statusCode == 200) {
            Assert.assertNotNull(header);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:networknt,項目名稱:light-4j,代碼行數:24,代碼來源:CorsHttpHandlerTest.java

示例2: testOptionsWrongOrigin

import org.apache.http.client.methods.HttpOptions; //導入方法依賴的package包/類
@Test
public void testOptionsWrongOrigin() throws Exception {
    String url = "http://localhost:8080";
    CloseableHttpClient client = HttpClients.createDefault();
    HttpOptions httpOptions = new HttpOptions(url);
    httpOptions.setHeader("Origin", "http://example.com");
    httpOptions.setHeader("Access-Control-Request-Method", "POST");
    httpOptions.setHeader("Access-Control-Request-Headers", "X-Requested-With");

    try {
        CloseableHttpResponse response = client.execute(httpOptions);
        int statusCode = response.getStatusLine().getStatusCode();
        String body = IOUtils.toString(response.getEntity().getContent(), "utf8");
        Header header = response.getFirstHeader("Access-Control-Allow-Origin");
        Assert.assertEquals(200, statusCode);
        if(statusCode == 200) {
            Assert.assertNull(header);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:networknt,項目名稱:light-4j,代碼行數:23,代碼來源:CorsHttpHandlerTest.java

示例3: testIsArbitraryMethodCacheableUsingSharedPublicCache

import org.apache.http.client.methods.HttpOptions; //導入方法依賴的package包/類
@Test
public void testIsArbitraryMethodCacheableUsingSharedPublicCache() {
    policy = new ResponseCachingPolicy(0, true, false, false);

    request = new HttpOptions("http://foo.example.com/");
    request.setHeader("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
    response.setStatusCode(HttpStatus.SC_NO_CONTENT);
    response.setHeader("Cache-Control", "public");

    Assert.assertFalse(policy.isResponseCacheable(request, response));
}
 
開發者ID:MyPureCloud,項目名稱:purecloud-iot,代碼行數:12,代碼來源:TestResponseCachingPolicy.java

示例4: testOptions

import org.apache.http.client.methods.HttpOptions; //導入方法依賴的package包/類
public void testOptions(String resource, String host) throws ClientProtocolException, IOException {
	CloseableHttpClient httpclient = HttpClients.createDefault();
	HttpOptions httpMethod = new HttpOptions(url + resource);
	if (host != null && !host.trim().isEmpty())
		httpMethod.setHeader("Host", host);
	System.out.println(httpMethod);
	HttpResponse response = httpclient.execute(httpMethod);

	System.out.println(response.toString());
	EntityUtils.consume(response.getEntity());
	System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
	httpclient.close();

}
 
開發者ID:regestaexe,項目名稱:bygle-ldp,代碼行數:15,代碼來源:HttpTest.java


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