本文整理匯總了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();
}
}
示例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();
}
}
示例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));
}
示例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();
}