本文整理汇总了Java中org.apache.http.client.methods.HttpUriRequest.addHeader方法的典型用法代码示例。如果您正苦于以下问题:Java HttpUriRequest.addHeader方法的具体用法?Java HttpUriRequest.addHeader怎么用?Java HttpUriRequest.addHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.methods.HttpUriRequest
的用法示例。
在下文中一共展示了HttpUriRequest.addHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getHttpClient
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
/**
* Get a valid {@link HttpClient} to use, with a valid token.
*
* @param login
* @return
*/
private CloseableHttpClient getHttpClient(final Boolean login, final HttpUriRequest request)
{
final HttpClientBuilder builder = HttpClientBuilder.create();
String key;
// if (apiClient.getApiToken() == null || apiClient.getApiToken())
if (login)
{
key = apiClient.getApiKey();
}
else
{
key = apiClient.getApiToken();
}
key += ":";
final byte[] credentials = Base64.encodeBase64(key.getBytes(StandardCharsets.UTF_8));
final BasicHeader authHeader = new BasicHeader("Authorization", "Basic " + new String(credentials, StandardCharsets.UTF_8));
request.addHeader(authHeader);
return builder.build();
}
示例2: sendRequest
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
/**
* Puts a new request in queue as a new thread in pool to be executed
*
* @param client HttpClient to be used for request, can differ in single requests
* @param contentType MIME body type, for POST and PUT requests, may be null
* @param context Context of Android application, to hold the reference of request
* @param httpContext HttpContext in which the request will be executed
* @param responseHandler ResponseHandler or its subclass to put the response into
* @param uriRequest instance of HttpUriRequest, which means it must be of HttpDelete,
* HttpPost, HttpGet, HttpPut, etc.
* @return RequestHandle of future request process
*/
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
if (contentType != null) {
uriRequest.addHeader("Content-Type", contentType);
}
responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
responseHandler.setRequestURI(uriRequest.getURI());
Future<?> request = threadPool.submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));
if (context != null) {
// Add request to request map
List<WeakReference<Future<?>>> requestList = requestMap.get(context);
if (requestList == null) {
requestList = new LinkedList<WeakReference<Future<?>>>();
requestMap.put(context, requestList);
}
requestList.add(new WeakReference<Future<?>>(request));
// TODO: Remove dead weakrefs from requestLists?
}
return new RequestHandle(request);
}
示例3: makeCommonHeaders
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
/**
* 共通のヘッダをセット.
* @param req リクエストオブジェクト
* @param contentType メディア種別
* @param accept Acceptヘッダ
* @param etag Etag値
*/
protected final void makeCommonHeaders(final HttpUriRequest req,
final String contentType,
final String accept,
final String etag) {
/*
* String token = accessor.getAccessToken(); if (!token.isEmpty()) { req.setHeader("Authorization",
* "Token token=\"" + token + "\""); } DaoConfig config = accessor.getDaoConfig(); String version =
* config.getDcVersion(); if (!"".equals(version)) { req.setHeader("X-Tritium-Version", version); }
*/
if (contentType != null) {
req.addHeader("Content-Type", contentType);
}
if (accept != null) {
req.addHeader("Accept", accept);
}
if (etag != null) {
req.addHeader("If-Match", etag);
}
}
示例4: sendRequest
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
@Override
protected RequestHandle sendRequest(DefaultHttpClient client,
HttpContext httpContext, HttpUriRequest uriRequest,
String contentType, ResponseHandlerInterface responseHandler,
Context context) {
if (contentType != null) {
uriRequest.addHeader(AsyncHttpClient.HEADER_CONTENT_TYPE, contentType);
}
responseHandler.setUseSynchronousMode(true);
/*
* will execute the request directly
*/
newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler, context).run();
// Return getUrl Request Handle that cannot be used to cancel the request
// because it is already complete by the time this returns
return new RequestHandle(null);
}
示例5: execute1
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
private static String execute1(HttpUriRequest httpUriRequest) throws Exception {
CloseableHttpResponse closeableHttpResponse = null;
httpUriRequest.addHeader("Connection", "close");
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().disableCookieManagement().disableConnectionState();
try {
httpClientBuilder.setSSLSocketFactory(SSLConnectionSocketFactory.getSocketFactory());
} catch (Exception var12) {
LOGGER.error(httpUriRequest.toString(), var12);
}
CloseableHttpClient httpClient = httpClientBuilder.build();
String var6;
try {
closeableHttpResponse = httpClient.execute(httpUriRequest);
String response = EntityUtils.toString(closeableHttpResponse.getEntity(), CHARSET);
var6 = response;
} catch (Exception var10) {
throw var10;
} finally {
CloseUtils.close(closeableHttpResponse);
CloseUtils.close(httpClient);
}
return var6;
}
示例6: request
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
private <T> HttpUriRequest request(String path, String... headers) {
HttpUriRequest req = new HttpGet(uri.resolve(path));
for (int i = 0; i < headers.length; i +=2) {
req.addHeader(headers[i], headers[i + 1]);
}
return req;
}
示例7: addHeaders
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
/**
* Add the given headers to the given HTTP request.
* @param httpRequest the request to add the headers to
* @param headers the headers to add
*/
static void addHeaders(HttpUriRequest httpRequest, HttpHeaders headers) {
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
String headerName = entry.getKey();
if (!headerName.equalsIgnoreCase(HTTP.CONTENT_LEN) &&
!headerName.equalsIgnoreCase(HTTP.TRANSFER_ENCODING)) {
for (String headerValue : entry.getValue()) {
httpRequest.addHeader(headerName, headerValue);
}
}
}
}
示例8: read
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
try {
final SDSApiClient client = session.getClient();
final HttpUriRequest request = new HttpGet(String.format("%s/nodes/files/%s/downloads", client.getBasePath(),
new SDSNodeIdProvider(session).getFileid(file, new DisabledListProgressListener())));
request.addHeader("X-Sds-Auth-Token", StringUtils.EMPTY);
if(status.isAppend()) {
final HttpRange range = HttpRange.withStatus(status);
final String header;
if(-1 == range.getEnd()) {
header = String.format("bytes=%d-", range.getStart());
}
else {
header = String.format("bytes=%d-%d", range.getStart(), range.getEnd());
}
if(log.isDebugEnabled()) {
log.debug(String.format("Add range header %s for file %s", header, file));
}
request.addHeader(new BasicHeader(HttpHeaders.RANGE, header));
// Disable compression
request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity"));
}
final HttpResponse response = client.getClient().execute(request);
switch(response.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
case HttpStatus.SC_PARTIAL_CONTENT:
return new HttpMethodReleaseInputStream(response);
default:
throw new HttpResponseExceptionMappingService().map(new HttpResponseException(
response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
}
}
catch(IOException e) {
throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file);
}
}
示例9: writeOutBoundHeaders
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
private static Map<String, String> writeOutBoundHeaders(final MultivaluedMap<String, Object> headers,
final HttpUriRequest request) {
final Map<String, String> stringHeaders = HeaderUtils.asStringHeadersSingleValue(headers);
for(final Map.Entry<String, String> e : stringHeaders.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
return stringHeaders;
}
示例10: sendRequest
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
HttpUriRequest uriRequest, String contentType,
ResponseHandlerInterface responseHandler, Context context) {
if (contentType != null) {
uriRequest.addHeader("Content-Type", contentType);
}
responseHandler.setUseSynchronousMode(true);
newAsyncHttpRequest(client, httpContext, uriRequest, contentType, responseHandler,
context).run();
return new RequestHandle(null);
}
示例11: addCookies
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
public void addCookies(HttpUriRequest req) {
if (Objects.nonNull(COOKIES)) {
for (Header h : COOKIES) {
req.addHeader(h);
}
}
}
示例12: makeGetRequest
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
/**
* GETメソッドのためのリクエストオブジェクト生成.
* @param url リクエスト対象のURL
* @param accept メディアタイプ
* @return リクエストオブジェクト
*/
protected final HttpUriRequest makeGetRequest(final String url, final String accept) {
HttpUriRequest request = new HttpGet(url);
makeCommonHeaders(request, null, accept, null);
request.addHeader("Accept-Encoding", "gzip");
return request;
}
示例13: get
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
/**
* レスポンスボディを受け取るGETメソッド.
* @param url リクエスト対象URL
* @param headers リクエストヘッダのハッシュマップ
* @return DcResponse型
* @throws PersoniumException DAO例外
*/
public final PersoniumResponse get(final String url, final HashMap<String, String> headers)
throws PersoniumException {
HttpUriRequest req = new HttpGet(url);
for (Map.Entry<String, String> entry : headers.entrySet()) {
req.setHeader(entry.getKey(), entry.getValue());
}
req.addHeader("X-Personium-Version", PersoniumCoreTestConfig.getCoreVersion());
debugHttpRequest(req, "");
PersoniumResponse res = this.request(req);
return res;
}
示例14: getAcceptEncodingGzip
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
/**
* レスポンスボディを受け取るGETメソッド.
* @param url リクエスト対象URL
* @param headers リクエストヘッダのハッシュマップ
* @return DcResponse型
* @throws PersoniumException DAO例外
*/
public final PersoniumResponse getAcceptEncodingGzip(final String url, final HashMap<String, String> headers)
throws PersoniumException {
HttpUriRequest req = new HttpGet(url);
req.addHeader("Accept-Encoding", "gzip");
for (Map.Entry<String, String> entry : headers.entrySet()) {
req.setHeader(entry.getKey(), entry.getValue());
}
req.addHeader("X-Personium-Version", PersoniumCoreTestConfig.getCoreVersion());
debugHttpRequest(req, "");
PersoniumResponse res = this.request(req);
return res;
}
示例15: put
import org.apache.http.client.methods.HttpUriRequest; //导入方法依赖的package包/类
/**
* レスポンスボディを受ける PUTメソッド.
* @param url リクエスト対象URL
* @param data 書き込むデータ
* @param headers リクエストヘッダのハッシュマップ
* @return DcResponse型
* @throws PersoniumException DAO例外
*/
public final PersoniumResponse put(final String url,
final String data,
final HashMap<String, String> headers) throws PersoniumException {
String contentType = headers.get(HttpHeaders.CONTENT_TYPE);
HttpUriRequest req = makePutRequest(url, data, contentType);
for (Map.Entry<String, String> entry : headers.entrySet()) {
req.setHeader(entry.getKey(), entry.getValue());
}
req.addHeader("X-Personium-Version", PersoniumCoreTestConfig.getCoreVersion());
debugHttpRequest(req, data);
PersoniumResponse res = request(req);
return res;
}