本文整理汇总了Java中org.apache.http.HttpRequest.addHeader方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequest.addHeader方法的具体用法?Java HttpRequest.addHeader怎么用?Java HttpRequest.addHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.HttpRequest
的用法示例。
在下文中一共展示了HttpRequest.addHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: determineProxy
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
@Override
protected HttpHost determineProxy(HttpHost host, HttpRequest request, HttpContext context) throws HttpException {
HttpClientContext httpClientContext = HttpClientContext.adapt(context);
Proxy proxy = proxyPlanner.determineProxy(host, request, context, ipPool, crawlerSession);
if (proxy == null) {
return null;
}
if (log.isDebugEnabled()) {
log.debug("{} 当前使用IP为:{}:{}", host.getHostName(), proxy.getIp(), proxy.getPort());
}
context.setAttribute(VSCRAWLER_AVPROXY_KEY, proxy);
if (proxy.getAuthenticationHeaders() != null) {
for (Header header : proxy.getAuthenticationHeaders()) {
request.addHeader(header);
}
}
if (StringUtils.isNotEmpty(proxy.getUsername()) && StringUtils.isNotEmpty(proxy.getPassword())) {
BasicCredentialsProvider credsProvider1 = new BasicCredentialsProvider();
httpClientContext.setCredentialsProvider(credsProvider1);
credsProvider1.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
}
return new HttpHost(proxy.getIp(), proxy.getPort());
}
示例2: process
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
public void process(HttpRequest request, HttpContext context) {
if (!request.containsHeader(AsyncHttpClient.HEADER_ACCEPT_ENCODING)) {
request.addHeader(AsyncHttpClient.HEADER_ACCEPT_ENCODING, AsyncHttpClient
.ENCODING_GZIP);
}
for (String header : AsyncHttpClient.access$000(this.this$0).keySet()) {
if (request.containsHeader(header)) {
Header overwritten = request.getFirstHeader(header);
Log.d(AsyncHttpClient.LOG_TAG, String.format("Headers were overwritten! (%s | %s)" +
" overwrites (%s | %s)", new Object[]{header, AsyncHttpClient.access$000
(this.this$0).get(header), overwritten.getName(), overwritten.getValue()}));
request.removeHeader(overwritten);
}
request.addHeader(header, (String) AsyncHttpClient.access$000(this.this$0).get(header));
}
}
示例3: addRequestInformation
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
public static void addRequestInformation(Subsegment subsegment, HttpRequest request, String url) {
subsegment.setNamespace(Namespace.REMOTE.toString());
Segment parentSegment = subsegment.getParentSegment();
TraceHeader header = new TraceHeader(parentSegment.getTraceId(),
parentSegment.isSampled() ? subsegment.getId() : null,
parentSegment.isSampled() ? SampleDecision.SAMPLED : SampleDecision.NOT_SAMPLED);
request.addHeader(TraceHeader.HEADER_KEY, header.toString());
Map<String, Object> requestInformation = new HashMap<>();
requestInformation.put("url", url);
requestInformation.put("method", request.getRequestLine().getMethod());
subsegment.putHttp("request", requestInformation);
}
示例4: process
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase("CONNECT")) {
return;
}
// Add default headers
@SuppressWarnings("unchecked")
Collection<Header> defHeaders = (Collection<Header>) request.getParams().getParameter(
ClientPNames.DEFAULT_HEADERS);
if (defHeaders != null) {
for (Header defHeader : defHeaders) {
request.addHeader(defHeader);
}
}
}
示例5: process
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase("CONNECT")) {
return;
}
if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) {
// Default policy is to keep connection alive
// whenever possible
request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
}
}
示例6: process
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
public void process(final HttpRequest request, final HttpContext context)
throws HttpException, IOException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
// Do not send the expect header if request body is known to be empty
if (entity != null && entity.getContentLength() != 0) {
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
if (HttpProtocolParams.useExpectContinue(request.getParams())
&& !ver.lessEquals(HttpVersion.HTTP_1_0)) {
request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE);
}
}
}
}
示例7: process
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
if(StringUtils.contains(request.getRequestLine().getUri(), "b2_authorize_account")) {
// Skip setting token for
if(log.isDebugEnabled()) {
log.debug("Skip setting token in b2_authorize_account");
}
return;
}
switch(request.getRequestLine().getMethod()) {
case "POST":
// Do not override Authorization header for upload requests with upload URL token
break;
default:
if(StringUtils.isNotBlank(authorizationToken)) {
request.removeHeaders(HttpHeaders.AUTHORIZATION);
request.addHeader(HttpHeaders.AUTHORIZATION, authorizationToken);
}
}
}
示例8: doGET
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
public HttpResponse doGET(URI url) throws IOException, HttpException {
String uri = url.getRawPath() + (url.getRawQuery() != null ? "?" + url.getRawQuery() : "");
HttpRequest request = new BasicHttpRequest("GET", uri);
String hostHeader = (url.getPort() == 0 || url.getPort() == 80)
? url.getHost() : (url.getHost() + ":" + url.getPort());
request.addHeader("Host", hostHeader);
return execute(request);
}
示例9: process
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
if(StringUtils.isNotBlank(token)) {
request.removeHeaders(SDSSession.SDS_AUTH_TOKEN_HEADER);
request.addHeader(SDSSession.SDS_AUTH_TOKEN_HEADER, token);
}
}
示例10: setAuthorizationHeader
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
private void setAuthorizationHeader( HttpRequest requestMethod )
{
/* First try basic auth */
String authHeader = getAuthenticationHeaderBasic( );
/* If null, try Oauth */
if ( authHeader == null )
{
authHeader = getAuthenticationHeaderBearer( );
}
/* Set header if not null, otherwise don't set header */
if ( authHeader != null )
{
requestMethod.addHeader( "Authorization", authHeader );
}
}
示例11: tryCloseConnection
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
private void tryCloseConnection(HttpRequest request) {
Header[] headers = request.getHeaders("Connection");
if (headers != null && headers.length > 0) {
for (Header h : headers) {
request.removeHeader(h);
}
}
request.addHeader("Connection", "close");
}
示例12: execute
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
@Override
public HttpResponse execute(final HttpRequest request, final HttpClientConnection conn, final HttpContext context)
throws IOException, HttpException {
if(!request.containsHeader(HttpHeaders.USER_AGENT)) {
request.addHeader(new BasicHeader(HttpHeaders.USER_AGENT, useragentProvider.get()));
}
return super.execute(request, conn, context);
}
示例13: process
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
@Override
public void process(HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
final String credentials = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8).trim();
httpRequest.addHeader(HttpHeaders.AUTHORIZATION, credentials);
}
示例14: modifyRequestToAcceptGzipResponse
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
public static void modifyRequestToAcceptGzipResponse(HttpRequest request) {
Timber.i("Requesting gzipped data");
request.addHeader("Accept-Encoding", "gzip");
}
示例15: process
import org.apache.http.HttpRequest; //导入方法依赖的package包/类
public void process(HttpRequest request, HttpContext context) {
if (!request.containsHeader("Accept-Encoding")) {
request.addHeader("Accept-Encoding", "gzip");
}
}