本文整理汇总了Java中org.apache.http.client.methods.HttpRequestBase.addHeader方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestBase.addHeader方法的具体用法?Java HttpRequestBase.addHeader怎么用?Java HttpRequestBase.addHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.methods.HttpRequestBase
的用法示例。
在下文中一共展示了HttpRequestBase.addHeader方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getHttp
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
public String getHttp(String url, List<NameValuePair> headers) throws IOException
{
HttpRequestBase request = new HttpGet(url);
if (headers != null)
{
for (NameValuePair header : headers)
{
request.addHeader(header.getName(), header.getValue());
}
}
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null)
{
return EntityUtils.toString(entity);
}
return null;
}
示例2: addHeadersToRequest
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
/**
* Configures the headers in the specified Apache HTTP request.
*/
private void addHeadersToRequest(HttpRequestBase httpRequest, SdkHttpFullRequest request) {
httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request));
// Copy over any other headers already in our request
request.headers().entrySet().stream()
/*
* HttpClient4 fills in the Content-Length header and complains if
* it's already present, so we skip it here. We also skip the Host
* header to avoid sending it twice, which will interfere with some
* signing schemes.
*/
.filter(e -> !IGNORE_HEADERS.contains(e.getKey()))
.forEach(e -> e.getValue().forEach(h -> httpRequest.addHeader(e.getKey(), h)));
/* Set content type and encoding */
if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null ||
httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0) {
httpRequest.addHeader(HttpHeaders.CONTENT_TYPE,
"application/x-www-form-urlencoded; " +
"charset=" + lowerCase(DEFAULT_ENCODING));
}
}
示例3: getNewRequest
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
private HttpRequestBase getNewRequest(String reqMethod, String reqPayload)
throws URISyntaxException, UnsupportedEncodingException {
HttpRequestBase request;
if(reqMethod.equals(HttpConstants.REQ_METHOD_POST)) {
HttpPost postRequest = new HttpPost();
postRequest.setEntity(new StringEntity(reqPayload, ContentType.create(DataFormats.JSON.getMediaType(), Constants.UTF_8)));
request = postRequest;
} else {
throw new IllegalArgumentException(Errors.ARGS_HTTP_METHOD_UNSUPPORTED.getDescription());
}
request.setURI(new URI(String.format("%s://%s:%s/",
nodeConfig.getProperty(NodeProps.RPC_PROTOCOL.getKey()),
nodeConfig.getProperty(NodeProps.RPC_HOST.getKey()),
nodeConfig.getProperty(NodeProps.RPC_PORT.getKey()))));
String authScheme = nodeConfig.getProperty(NodeProps.HTTP_AUTH_SCHEME.getKey());
request.addHeader(resolveAuthHeader(authScheme));
LOG.debug("<< getNewRequest(..): returning a new HTTP '{}' request with target endpoint "
+ "'{}' and headers '{}'", reqMethod, request.getURI(), request.getAllHeaders());
return request;
}
示例4: performRequest
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
public CloseableHttpResponse performRequest(HttpRequestBase request, boolean revalidate) {
String method = request.getMethod();
if (revalidate) {
request.addHeader(HttpHeaders.CACHE_CONTROL, "max-age=0");
}
CloseableHttpResponse response;
try {
response = executeGetOrHead(request);
} catch (IOException e) {
throw new HttpRequestException(String.format("Could not %s '%s'.", method, request.getURI()), e);
}
return response;
}
示例5: commitHeaders
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
protected void commitHeaders(ClientInvocation request, HttpRequestBase httpMethod) {
MultivaluedMap<String, String> headers = request.getHeaders().asMap();
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
List<String> values = header.getValue();
for (String value : values) {
// System.out.println(String.format("setting %s = %s", header.getKey(), value));
httpMethod.addHeader(header.getKey(), value);
}
}
}
示例6: addAuth
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
@Override
public void addAuth(HttpRequestBase request, CookieStore cookieStore) {
String basicAuthUnencoded = String.format("%s:%s", username, password);
String basicAuth = "Basic " + BaseEncoding.base64().encode(basicAuthUnencoded.getBytes());
request.addHeader("Authorization", basicAuth);
}
示例7: executeRequest
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
private <R> CompletableFuture<R> executeRequest(final ResponseParser<R> parser, HttpRequestBase request)
throws IOException, ClientProtocolException
{
request.addHeader("OCS-APIRequest", "true");
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
request.setProtocolVersion(HttpVersion.HTTP_1_1);
HttpClientContext context = prepareContext();
CompletableFuture<R> futureResponse = new CompletableFuture<>();
HttpAsyncClientSingleton.httpclient.execute(request, context, new ResponseCallback<R>(parser, futureResponse));
return futureResponse;
}
示例8: addHeadersToHttpMethod
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
/**
* Pass the user-specified HTTP headers to the Apache HTTP Components
* method object.
*/
protected void addHeadersToHttpMethod( HttpRequestBase httpMethod ) {
for (HttpHeader header : requestHeaders) {
for (String value : header.getValues()) {
httpMethod.addHeader(header.getKey(), value);
}
}
}
示例9: getRequest
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
public HttpRequestBase getRequest(){
String url = getUrl();
HttpRequestBase request = getRequest(url);
if(!headers.isEmpty()){
for(Entry<String,List<String>> header : headers.entrySet()){
for(String headerValue : header.getValue()){
request.addHeader(header.getKey(), headerValue);
}
}
}
if(entity != null && canHaveEntity()){
((HttpEntityEnclosingRequest) request).setEntity(entity);
}
if(timeoutMs != null || proxy != null){
Builder builder = RequestConfig.custom();
builder.setCookieSpec(CookieSpecs.STANDARD);
if(timeoutMs != null){
builder.setConnectTimeout(timeoutMs).setConnectionRequestTimeout(timeoutMs).setSocketTimeout(timeoutMs);
}
if(proxy != null){
builder.setProxy(proxy);
}
RequestConfig requestConfig = builder.build();
request.setConfig(requestConfig);
}
return request;
}
示例10: addCookies
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
private void addCookies(HttpRequestBase method, Cookies cookies)
{
for( ACookie cookie : cookies.send )
{
method.addHeader("Cookie", cookie.toString());
}
}
示例11: handlerHeader
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
/**
* @param header
* @param httpRequestBase
*/
private void handlerHeader(Map<String, String> header, HttpRequestBase httpRequestBase) {
if (Check.isNullOrEmpty(header)) {
return;
}
Iterator<String> iterator = header.keySet().iterator();
while (iterator.hasNext()) {
String headerName = iterator.next();
httpRequestBase.addHeader(headerName, header.get(headerName));
}
}
示例12: addAuthHeadersToRequest
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
private void addAuthHeadersToRequest(HttpRequestBase request) {
if (authHeaderProducer == null) {
return;
}
Map<String, String> authHeaders = authHeaderProducer.getAuthHeaders();
for (String headerName : authHeaders.keySet()) {
request.addHeader(headerName, authHeaders.get(headerName));
}
}
示例13: addHeadersToRequest
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
/**
* Configures the headers in the specified Apache HTTP request.
*/
private void addHeadersToRequest(HttpRequestBase httpRequest, Request<?> request) {
httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request.getEndpoint()));
// Copy over any other headers already in our request
for (Entry<String, String> entry : request.getHeaders().entrySet()) {
/*
* HttpClient4 fills in the Content-Length header and complains if
* it's already present, so we skip it here. We also skip the Host
* header to avoid sending it twice, which will interfere with some
* signing schemes.
*/
if (!(ignoreHeaders.contains(entry.getKey()))) {
httpRequest.addHeader(entry.getKey(), entry.getValue());
}
}
/* Set content type and encoding */
if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest
.getHeaders
(HttpHeaders.CONTENT_TYPE).length == 0) {
httpRequest.addHeader(HttpHeaders.CONTENT_TYPE,
"application/x-www-form-urlencoded; " +
"charset=" + DEFAULT_ENCODING.toLowerCase());
}
}