本文整理汇总了Java中org.apache.http.HttpMessage.addHeader方法的典型用法代码示例。如果您正苦于以下问题:Java HttpMessage.addHeader方法的具体用法?Java HttpMessage.addHeader怎么用?Java HttpMessage.addHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.HttpMessage
的用法示例。
在下文中一共展示了HttpMessage.addHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: moveMIMEHeadersToHTTPHeader
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
public static void moveMIMEHeadersToHTTPHeader (@Nonnull final MimeMessage aMimeMsg,
@Nonnull final HttpMessage aHttpMsg) throws MessagingException
{
ValueEnforcer.notNull (aMimeMsg, "MimeMsg");
ValueEnforcer.notNull (aHttpMsg, "HttpMsg");
// Move all mime headers to the HTTP request
final Enumeration <Header> aEnum = aMimeMsg.getAllHeaders ();
while (aEnum.hasMoreElements ())
{
final Header h = aEnum.nextElement ();
// Make a single-line HTTP header value!
aHttpMsg.addHeader (h.getName (), HttpHeaderMap.getUnifiedValue (h.getValue ()));
// Remove from MIME message!
aMimeMsg.removeHeader (h.getName ());
}
}
示例2: completeRequest
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
/** helper function to fill the request with header entries .
* */
private static HttpMessage completeRequest(HttpMessage request,
List<NameValuePair> headerEntries){
if (null == request){
logger.error("unable to complete request as the passed request object is null");
return request;
}
// dump all the header entries to the request.
if (null != headerEntries && headerEntries.size() > 0){
for (NameValuePair pair : headerEntries){
request.addHeader(pair.getName(), pair.getValue());
}
}
return request;
}
示例3: setHeaders
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
protected void setHeaders(final HttpMessage base, final String headers) throws IOException {
try (BufferedReader reader = new BufferedReader(new StringReader(headers))) {
String line = reader.readLine();
while (line != null) {
int colonIndex = line.indexOf(":");
if (colonIndex > 0) {
String headerName = line.substring(0, colonIndex);
if (line.length() > colonIndex + 2) {
base.addHeader(headerName, line.substring(colonIndex + 1));
} else {
base.addHeader(headerName, null);
}
line = reader.readLine();
} else {
throw new FlowableException(HTTP_TASK_REQUEST_HEADERS_INVALID);
}
}
}
}
示例4: addCMCHeaders
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
/**
* Helper method to set the HTTP headers required for all HTTP requests.
*
* @param httpMessage HTTP message
*/
private void addCMCHeaders(HttpMessage httpMessage) {
// Set the Accepts and Content Type headers
httpMessage.addHeader(HttpHeaders.ACCEPT, "application/json");
httpMessage.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
// Custom header for CSRF protection
httpMessage.addHeader("X-Requested-By", "12345");
// Add a user agent header
httpMessage.addHeader("User-Agent", "cmc-java " + CMC_VERSION);
// Set the basic authorization headers.
String auth = accountID + ":" + authenticationToken;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.US_ASCII));
String authHeader = "Basic " + new String(encodedAuth, StandardCharsets.US_ASCII);
httpMessage.addHeader(HttpHeaders.AUTHORIZATION, authHeader);
}
示例5: getCoapOptionsContentTypeTest
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
@Test
public final void getCoapOptionsContentTypeTest() {
// create the message
HttpMessage httpMessage = new BasicHttpRequest("get", "http://localhost");
// create the header
String headerName = "content-type";
String headerValue = "text/plain";
Header header = new BasicHeader(headerName, headerValue);
httpMessage.addHeader(header);
// translate the header
List<Option> options = HttpTranslator.getCoapOptions(httpMessage.getAllHeaders());
// the context-type should not be handled by this method
assertTrue(options.isEmpty());
}
示例6: getCoapOptionsMaxAgeTest
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
@Test
public final void getCoapOptionsMaxAgeTest() {
// create the message
HttpMessage httpMessage = new BasicHttpRequest("get", "http://localhost");
// create the header
String headerName = "cache-control";
int maxAge = 25;
String headerValue = "max-age=" + maxAge;
Header header = new BasicHeader(headerName, headerValue);
httpMessage.addHeader(header);
// translate the header
List<Option> options = HttpTranslator.getCoapOptions(httpMessage.getAllHeaders());
assertFalse(options.isEmpty());
assertTrue(options.size() == 1);
// get the option list
Message coapMessage = new GETRequest();
coapMessage.setOptions(options);
Option testedOption = coapMessage.getFirstOption(OptionNumberRegistry.MAX_AGE);
assertNotNull(testedOption);
assertEquals(maxAge, testedOption.getIntValue());
}
示例7: getCoapOptionsMaxAgeTest2
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
@Test
public final void getCoapOptionsMaxAgeTest2() {
// create the message
HttpMessage httpMessage = new BasicHttpRequest("get", "http://localhost");
// create the header
String headerName = "cache-control";
String headerValue = "no-cache";
Header header = new BasicHeader(headerName, headerValue);
httpMessage.addHeader(header);
// translate the header
List<Option> options = HttpTranslator.getCoapOptions(httpMessage.getAllHeaders());
assertFalse(options.isEmpty());
assertTrue(options.size() == 1);
// get the option list
Message coapMessage = new GETRequest();
coapMessage.setOptions(options);
Option testedOption = coapMessage.getFirstOption(OptionNumberRegistry.MAX_AGE);
assertNotNull(testedOption);
assertEquals(0, testedOption.getIntValue());
}
示例8: getCoapOptionsTest
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
/**
* Test method for
* {@link ch.ethz.inf.vs.californium.util.HttpTranslator#getCoapOptions(org.apache.http.HttpMessage)}
* .
*/
@Test
public final void getCoapOptionsTest() {
// create the message
HttpMessage httpMessage = new BasicHttpRequest("get", "http://localhost");
// create the header
String headerName = "if-match";
String headerValue = "\"737060cd8c284d8af7ad3082f209582d\"";
Header header = new BasicHeader(headerName, headerValue);
httpMessage.addHeader(header);
// translate the header
List<Option> options = HttpTranslator.getCoapOptions(httpMessage.getAllHeaders());
assertFalse(options.isEmpty());
// get the option list
Message coapMessage = new GETRequest();
coapMessage.setOptions(options);
int optionNumber = Integer.parseInt(HttpTranslator.HTTP_TRANSLATION_PROPERTIES.getProperty("http.message.header." + headerName));
assertEquals(coapMessage.getFirstOption(optionNumber).getStringValue(), headerValue);
}
示例9: add
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
public static void add(HttpMessage httpMessage, Headers headers) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
for (String value : entry.getValue()) {
httpMessage.addHeader(entry.getKey(), value);
}
}
}
示例10: getCoapOptionsAcceptTest
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
@Test
public final void getCoapOptionsAcceptTest() {
// create the header
String headerName = "accept";
String headerValue = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
Header header = new BasicHeader(headerName, headerValue);
// create the message
HttpMessage httpMessage = new BasicHttpRequest("get", "http://localhost");
httpMessage.addHeader(header);
// translate the header
List<Option> options = HttpTranslator.getCoapOptions(httpMessage.getAllHeaders());
assertFalse(options.isEmpty());
assertTrue(options.size() == 2);
// get the option list
Message coapMessage = new GETRequest();
coapMessage.setOptions(options);
List<Option> testedOptions = coapMessage.getOptions(OptionNumberRegistry.ACCEPT);
assertFalse(testedOptions.isEmpty());
// only 2 to 3 content-types are translated
assertTrue(testedOptions.size() == 2);
assertTrue(testedOptions.contains(new Option(MediaTypeRegistry.TEXT_HTML, OptionNumberRegistry.ACCEPT)));
assertTrue(testedOptions.contains(new Option(MediaTypeRegistry.APPLICATION_XML, OptionNumberRegistry.ACCEPT)));
}
示例11: getCoapOptionsAcceptWildcardTest
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
@Test
public final void getCoapOptionsAcceptWildcardTest() {
// create the header
String headerName = "accept";
String headerValue = "text/*";
Header header = new BasicHeader(headerName, headerValue);
// create the message
HttpMessage httpMessage = new BasicHttpRequest("get", "http://localhost");
httpMessage.addHeader(header);
// translate the header
List<Option> options = HttpTranslator.getCoapOptions(httpMessage.getAllHeaders());
assertFalse(options.isEmpty());
// get the option list
Message coapMessage = new GETRequest();
coapMessage.setOptions(options);
int optionNumber = Integer.parseInt(HttpTranslator.HTTP_TRANSLATION_PROPERTIES.getProperty("http.message.header." + headerName));
List<Option> testedOptions = coapMessage.getOptions(optionNumber);
assertFalse(testedOptions.isEmpty());
// check content-types translated
for (Integer mediaType : MediaTypeRegistry.parseWildcard(headerValue)) {
assertTrue(testedOptions.contains(new Option(mediaType, OptionNumberRegistry.ACCEPT)));
}
}
示例12: getCoapOptionsNotFoundTest
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
@Test
public final void getCoapOptionsNotFoundTest() {
// create the header
String headerName = "unknown-header";
String headerValue = "aaaa";
Header header = new BasicHeader(headerName, headerValue);
// create the message
HttpMessage httpMessage = new BasicHttpRequest("get", "http://localhost");
httpMessage.addHeader(header);
// translate the header
List<Option> options = HttpTranslator.getCoapOptions(httpMessage.getAllHeaders());
assertTrue(options.isEmpty());
}
示例13: addHeaders
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
public void addHeaders(HttpMessage request) {
for(SimpleImmutableEntry<String, String> header : url.getHeaders()) {
request.addHeader(header.getKey(), header.getValue());
}
}
示例14: addHeadersToRequest
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
private void addHeadersToRequest(HttpMessage apacheRequest)
{
for(MattpHeader header : request.getHeaders())
apacheRequest.addHeader(header.getKey(), header.getValue());
}
示例15: addBodyToRequest
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
private void addBodyToRequest(HttpMessage apacheRequest)
{
apacheRequest.addHeader("Content-Type", request.getBody().getContentType());
((HttpEntityEnclosingRequest) apacheRequest).setEntity(new InputStreamEntity(request.getBody().getAsInputStream()));
}