本文整理汇总了Java中org.apache.http.HttpMessage类的典型用法代码示例。如果您正苦于以下问题:Java HttpMessage类的具体用法?Java HttpMessage怎么用?Java HttpMessage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpMessage类属于org.apache.http包,在下文中一共展示了HttpMessage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: serialize
import org.apache.http.HttpMessage; //导入依赖的package包/类
/**
* Writes out the content of the given HTTP entity to the session output
* buffer based on properties of the given HTTP message.
*
* @param outbuffer the output session buffer.
* @param message the HTTP message.
* @param entity the HTTP entity to be written out.
* @throws HttpException in case of HTTP protocol violation.
* @throws IOException in case of an I/O error.
*/
public void serialize(
final SessionOutputBuffer outbuffer,
final HttpMessage message,
final HttpEntity entity) throws HttpException, IOException {
if (outbuffer == null) {
throw new IllegalArgumentException("Session output buffer may not be null");
}
if (message == null) {
throw new IllegalArgumentException("HTTP message may not be null");
}
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
OutputStream outstream = doSerialize(outbuffer, message);
entity.writeTo(outstream);
outstream.close();
}
示例2: 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 ());
}
}
示例3: 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;
}
示例4: 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);
}
}
}
}
示例5: generateViaHeader
import org.apache.http.HttpMessage; //导入依赖的package包/类
private String generateViaHeader(final HttpMessage msg) {
final ProtocolVersion pv = msg.getProtocolVersion();
final String existingEntry = viaHeaders.get(pv);
if (existingEntry != null) {
return existingEntry;
}
final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.http.client", getClass().getClassLoader());
final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE;
String value;
if ("http".equalsIgnoreCase(pv.getProtocol())) {
value = String.format("%d.%d localhost (Apache-HttpClient/%s (cache))", pv.getMajor(), pv.getMinor(),
release);
} else {
value = String.format("%s/%d.%d localhost (Apache-HttpClient/%s (cache))", pv.getProtocol(), pv.getMajor(),
pv.getMinor(), release);
}
viaHeaders.put(pv, value);
return value;
}
示例6: generateViaHeader
import org.apache.http.HttpMessage; //导入依赖的package包/类
private String generateViaHeader(final HttpMessage msg) {
final ProtocolVersion pv = msg.getProtocolVersion();
final String existingEntry = viaHeaders.get(pv);
if (existingEntry != null) {
return existingEntry;
}
final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.http.client", getClass().getClassLoader());
final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE;
String value;
final int major = pv.getMajor();
final int minor = pv.getMinor();
if ("http".equalsIgnoreCase(pv.getProtocol())) {
value = String.format("%d.%d localhost (Apache-HttpClient/%s (cache))", major, minor,
release);
} else {
value = String.format("%s/%d.%d localhost (Apache-HttpClient/%s (cache))", pv.getProtocol(), major,
minor, release);
}
viaHeaders.put(pv, value);
return value;
}
示例7: 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);
}
示例8: recordCookie
import org.apache.http.HttpMessage; //导入依赖的package包/类
protected void recordCookie(HttpMessage httpMessage, Trace trace) {
org.apache.http.Header[] cookies = httpMessage.getHeaders("Cookie");
for (org.apache.http.Header header : cookies) {
final String value = header.getValue();
if (StringUtils.hasLength(value)) {
if (cookieSampler.isSampling()) {
final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
recorder.recordAttribute(AnnotationKey.HTTP_COOKIE, StringUtils.abbreviate(value, 1024));
}
// Can a cookie have 2 or more values?
// PMD complains if we use break here
return;
}
}
}
示例9: recordEntity
import org.apache.http.HttpMessage; //导入依赖的package包/类
protected void recordEntity(HttpMessage httpMessage, Trace trace) {
if (httpMessage instanceof HttpEntityEnclosingRequest) {
final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) httpMessage;
try {
final HttpEntity entity = entityRequest.getEntity();
if (entity != null && entity.isRepeatable() && entity.getContentLength() > 0) {
if (entitySampler.isSampling()) {
final String entityString = entityUtilsToString(entity, Charsets.UTF_8_NAME, 1024);
final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
recorder.recordAttribute(AnnotationKey.HTTP_PARAM_ENTITY, entityString);
}
}
} catch (Exception e) {
logger.debug("HttpEntityEnclosingRequest entity record fail. Caused:{}", e.getMessage(), e);
}
}
}
示例10: recordEntity
import org.apache.http.HttpMessage; //导入依赖的package包/类
protected void recordEntity(HttpMessage httpMessage, SpanEventRecorder recorder) {
if (httpMessage instanceof HttpEntityEnclosingRequest) {
final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) httpMessage;
try {
final HttpEntity entity = entityRequest.getEntity();
if (entity != null && entity.isRepeatable() && entity.getContentLength() > 0) {
if (entitySampler.isSampling()) {
final String entityString = entityUtilsToString(entity, Charsets.UTF_8_NAME, 1024);
recorder.recordAttribute(AnnotationKey.HTTP_PARAM_ENTITY, entityString);
}
}
} catch (Exception e) {
logger.debug("HttpEntityEnclosingRequest entity record fail. Caused:{}", e.getMessage(), e);
}
}
}
示例11: 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());
}
示例12: 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());
}
示例13: 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());
}
示例14: 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);
}
示例15: setHeaders
import org.apache.http.HttpMessage; //导入依赖的package包/类
/**
* Sets the headers of an HTTP request necessary to execute.
*
* @param httpMessage The HTTP request to add the basic headers.
* @param headers A map of key-value pairs representing the headers.
*/
public static void setHeaders( HttpMessage httpMessage, Map<String,String> headers )
{
if( headers == null )
{
httpMessage.setHeader( "Accept-Language", "en-us,en;q=0.5" );
return;
}
else if( !headers.containsKey( "Accept-Language" ) )
{
headers.put( "Accept-Language", "en-us,en;q=0.5" );
}
for( Map.Entry<String, String> entry : headers.entrySet() )
{
httpMessage.setHeader( entry.getKey(), entry.getValue() );
}
}