本文整理汇总了Java中org.apache.http.HttpMessage.getFirstHeader方法的典型用法代码示例。如果您正苦于以下问题:Java HttpMessage.getFirstHeader方法的具体用法?Java HttpMessage.getFirstHeader怎么用?Java HttpMessage.getFirstHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.HttpMessage
的用法示例。
在下文中一共展示了HttpMessage.getFirstHeader方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doDeserialize
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
/**
* Creates a {@link BasicHttpEntity} based on properties of the given
* message. The content of the entity is created by wrapping
* {@link SessionInputBuffer} with a content decoder depending on the
* transfer mechanism used by the message.
* <p>
* This method is called by the public
* {@link #deserialize(SessionInputBuffer, HttpMessage)}.
*
* @param inbuffer the session input buffer.
* @param message the message.
* @return HTTP entity.
* @throws HttpException in case of HTTP protocol violation.
* @throws IOException in case of an I/O error.
*/
protected BasicHttpEntity doDeserialize(
final SessionInputBuffer inbuffer,
final HttpMessage message) throws HttpException, IOException {
BasicHttpEntity entity = new BasicHttpEntity();
long len = this.lenStrategy.determineLength(message);
if (len == ContentLengthStrategy.CHUNKED) {
entity.setChunked(true);
entity.setContentLength(-1);
entity.setContent(new ChunkedInputStream(inbuffer));
} else if (len == ContentLengthStrategy.IDENTITY) {
entity.setChunked(false);
entity.setContentLength(-1);
entity.setContent(new IdentityInputStream(inbuffer));
} else {
entity.setChunked(false);
entity.setContentLength(len);
entity.setContent(new ContentLengthInputStream(inbuffer, len));
}
Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
if (contentTypeHeader != null) {
entity.setContentType(contentTypeHeader);
}
Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
if (contentEncodingHeader != null) {
entity.setContentEncoding(contentEncodingHeader);
}
return entity;
}
示例2: prepareInput
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
protected HttpEntity prepareInput(final HttpMessage message) throws HttpException {
final BasicHttpEntityHC4 entity = new BasicHttpEntityHC4();
final long len = this.incomingContentStrategy.determineLength(message);
final InputStream instream = createInputStream(len, this.inbuffer);
if (len == ContentLengthStrategy.CHUNKED) {
entity.setChunked(true);
entity.setContentLength(-1);
entity.setContent(instream);
} else if (len == ContentLengthStrategy.IDENTITY) {
entity.setChunked(false);
entity.setContentLength(-1);
entity.setContent(instream);
} else {
entity.setChunked(false);
entity.setContentLength(len);
entity.setContent(instream);
}
final Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
if (contentTypeHeader != null) {
entity.setContentType(contentTypeHeader);
}
final Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
if (contentEncodingHeader != null) {
entity.setContentEncoding(contentEncodingHeader);
}
return entity;
}
示例3: get
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
protected String get(final String headerName, final HttpMessage message) {
Header header = message.getFirstHeader(headerName);
if (header!=null)
return header.getValue();
else
return "";
}
示例4: processSsdpPacket
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
private void processSsdpPacket(DatagramPacket packet) {
byte[] data = new byte[packet.getLength()];
System.arraycopy(packet.getData(), packet.getOffset(), data, 0, packet.getLength());
HttpMessage message = getHttpMessage(data);
if (message == null) return;
Header mandatoryExtensionHeader = message.getFirstHeader("man");
if (mandatoryExtensionHeader == null || !mandatoryExtensionHeader.getValue().equals("\"ssdp:discover\""))
return;
Header serviceTypeHeader = message.getFirstHeader("st");
if (serviceTypeHeader == null) return;
if (serviceTypeHeader.getValue().equals("ssdp:all") || serviceTypeHeader.getValue().equals(serviceType)) {
// respond
String ip = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
String response = String.format(responseTemplate, getServiceId(), ip, getPort());
DatagramPacket responsePacket = new DatagramPacket(response.getBytes(), response.length(), packet.getAddress(), packet.getPort());
try {
DatagramSocket sendingSocket = new DatagramSocket();
sendingSocket.send(responsePacket);
sendingSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例5: getHeader
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
public static String getHeader(HttpMessage message, String name) {
final Header header = message.getFirstHeader(name);
if (header != null) {
return header.getValue();
} else {
return null;
}
}
示例6: determineLength
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
public long determineLength(final HttpMessage message) throws HttpException {
if (message == null) {
throw new IllegalArgumentException("HTTP message may not be null");
}
HttpParams params = message.getParams();
boolean strict = params.isParameterTrue(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
// We use Transfer-Encoding if present and ignore Content-Length.
// RFC2616, 4.4 item number 3
if (transferEncodingHeader != null) {
HeaderElement[] encodings = null;
try {
encodings = transferEncodingHeader.getElements();
} catch (ParseException px) {
throw new ProtocolException
("Invalid Transfer-Encoding header value: " +
transferEncodingHeader, px);
}
if (strict) {
// Currently only chunk and identity are supported
for (int i = 0; i < encodings.length; i++) {
String encoding = encodings[i].getName();
if (encoding != null && encoding.length() > 0
&& !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING)
&& !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
throw new ProtocolException("Unsupported transfer encoding: " + encoding);
}
}
}
// The chunked encoding must be the last one applied RFC2616, 14.41
int len = encodings.length;
if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
return IDENTITY;
} else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(
encodings[len - 1].getName()))) {
return CHUNKED;
} else {
if (strict) {
throw new ProtocolException("Chunk-encoding must be the last one applied");
}
return IDENTITY;
}
}
Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
if (contentLengthHeader != null) {
long contentlen = -1;
Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
if (strict && headers.length > 1) {
throw new ProtocolException("Multiple content length headers");
}
for (int i = headers.length - 1; i >= 0; i--) {
Header header = headers[i];
try {
contentlen = Long.parseLong(header.getValue());
break;
} catch (NumberFormatException e) {
if (strict) {
throw new ProtocolException("Invalid content length: " + header.getValue());
}
}
// See if we can have better luck with another header, if present
}
if (contentlen >= 0) {
return contentlen;
} else {
return IDENTITY;
}
}
return this.implicitLen;
}
示例7: determineLength
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
public long determineLength(final HttpMessage message) throws HttpException {
Args.notNull(message, "HTTP message");
final Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
// We use Transfer-Encoding if present and ignore Content-Length.
// RFC2616, 4.4 item number 3
if (transferEncodingHeader != null) {
final HeaderElement[] encodings;
try {
encodings = transferEncodingHeader.getElements();
} catch (final ParseException px) {
throw new ProtocolException
("Invalid Transfer-Encoding header value: " +
transferEncodingHeader, px);
}
// The chunked encoding must be the last one applied RFC2616, 14.41
final int len = encodings.length;
if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
return IDENTITY;
} else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(
encodings[len - 1].getName()))) {
return CHUNKED;
} else {
return IDENTITY;
}
}
final Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
if (contentLengthHeader != null) {
long contentlen = -1;
final Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
for (int i = headers.length - 1; i >= 0; i--) {
final Header header = headers[i];
try {
contentlen = Long.parseLong(header.getValue());
break;
} catch (final NumberFormatException ignore) {
}
// See if we can have better luck with another header, if present
}
if (contentlen >= 0) {
return contentlen;
} else {
return IDENTITY;
}
}
return this.implicitLen;
}
示例8: transferFirstHeader
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
protected void transferFirstHeader(final String headerName, final String destName, final HttpMessage origin, final HttpMessage dest) {
Header header = origin.getFirstHeader(headerName);
if (header!=null)
dest.setHeader(destName, header.getValue());
}
示例9: getCoapMediaType
import org.apache.http.HttpMessage; //导入方法依赖的package包/类
/**
* Gets the coap media type associated to the http entity. Firstly, it looks
* for a valid mapping in the property file. If this step fails, then it
* tries to explicitly map/parse the declared mime/type by the http entity.
* If even this step fails, it sets application/octet-stream as
* content-type.
*
* @param httpMessage
*
*
* @return the coap media code associated to the http message entity. * @see
* HttpHeader, ContentType, MediaTypeRegistry
*/
public static int getCoapMediaType(HttpMessage httpMessage) {
if (httpMessage == null) {
throw new IllegalArgumentException("httpMessage == null");
}
// get the entity
HttpEntity httpEntity = null;
if (httpMessage instanceof HttpResponse) {
httpEntity = ((HttpResponse) httpMessage).getEntity();
} else if (httpMessage instanceof HttpEntityEnclosingRequest) {
httpEntity = ((HttpEntityEnclosingRequest) httpMessage).getEntity();
}
// check that the entity is actually present in the http message
if (httpEntity == null) {
throw new IllegalArgumentException("The http message does not contain any httpEntity.");
}
// set the content-type with a default value
int coapContentType = MediaTypeRegistry.UNDEFINED;
// get the content-type from the entity
ContentType contentType = ContentType.get(httpEntity);
if (contentType == null) {
// if the content-type is not set, search in the headers
Header contentTypeHeader = httpMessage.getFirstHeader("content-type");
if (contentTypeHeader != null) {
String contentTypeString = contentTypeHeader.getValue();
contentType = ContentType.parse(contentTypeString);
}
}
// check if there is an associated content-type with the current http
// message
if (contentType != null) {
// get the value of the content-type
String httpContentTypeString = contentType.getMimeType();
// delete the last part (if any)
httpContentTypeString = httpContentTypeString.split(";")[0];
// retrieve the mapping from the property file
String coapContentTypeString = HTTP_TRANSLATION_PROPERTIES.getProperty(KEY_HTTP_CONTENT_TYPE + httpContentTypeString);
if (coapContentTypeString != null) {
coapContentType = Integer.parseInt(coapContentTypeString);
} else {
// try to parse the media type if the property file has given to
// mapping
coapContentType = MediaTypeRegistry.parse(httpContentTypeString);
}
}
// if not recognized, the content-type should be
// application/octet-stream (draft-castellani-core-http-mapping 6.2)
if (coapContentType == MediaTypeRegistry.UNDEFINED) {
coapContentType = MediaTypeRegistry.APPLICATION_OCTET_STREAM;
}
return coapContentType;
}