本文整理汇总了Java中org.apache.http.client.methods.HttpRequestBase.setHeader方法的典型用法代码示例。如果您正苦于以下问题:Java HttpRequestBase.setHeader方法的具体用法?Java HttpRequestBase.setHeader怎么用?Java HttpRequestBase.setHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.methods.HttpRequestBase
的用法示例。
在下文中一共展示了HttpRequestBase.setHeader方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addAuthorizationToRequest
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
/**
* Sends request to NGB server, retrieves an authorization token and adds it to an input request.
* This is required for secure requests.
* @param request to authorize
*/
protected void addAuthorizationToRequest(HttpRequestBase request) {
try {
HttpPost post = new HttpPost(serverParameters.getServerUrl() + serverParameters.getAuthenticationUrl());
StringEntity input = new StringEntity(serverParameters.getAuthPayload());
input.setContentType(APPLICATION_JSON);
post.setEntity(input);
post.setHeader(CACHE_CONTROL, CACHE_CONTROL_NO_CACHE);
post.setHeader(CONTENT_TYPE, "application/x-www-form-urlencoded");
String result = RequestManager.executeRequest(post);
Authentication authentication = getMapper().readValue(result, Authentication.class);
request.setHeader("authorization", "Bearer " + authentication.getAccessToken());
} catch (IOException e) {
throw new ApplicationException("Failed to authenticate request", e);
}
}
示例2: addRequiredHeader
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
@Override
protected void addRequiredHeader(HttpRequestBase httpRequestBase) {
if (!report.isAnonymousTest()) {
String token = report.getToken();
if (token != null && token.contains(":")) {
httpRequestBase.setHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes())));
} else {
httpRequestBase.setHeader("X-Api-Key", token);
}
}
}
示例3: setHeader
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
/**
* 设置请求头
*
* @author gaoxianglong
*/
public static void setHeader(HttpRequestBase request, String headers, ContentType contentType, String charset) {
if (null == request) {
return;
}
request.setHeader(HTTP.CONTENT_TYPE,
Optional.ofNullable(contentType.type).orElseGet(() -> ContentType.APPLICATION_XML.type) + ";"
+ Optional.ofNullable(charset).orElseGet(() -> "UTF-8"));
if (!StringUtils.isEmpty(headers)) {
/* 设置请求头参数 */
JSONObject jsonObj = JSONObject.parseObject(JSONObject.parseObject(headers).get("header").toString());
jsonObj.keySet().stream().forEach(key -> request.setHeader(key, jsonObj.getString(key)));
}
}
示例4: applyDefaultHeaders
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
public static void applyDefaultHeaders(HttpRequestBase httpRequestBase){
httpRequestBase.setHeader("Accept", "text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8");
httpRequestBase.setHeader("Accept-Encoding", "gzip, deflate");
httpRequestBase.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
httpRequestBase.setHeader("Connection", "keep-alive");
httpRequestBase.setHeader("DNT", "1");
httpRequestBase.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0");
}
示例5: setRequestHeader
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
private void setRequestHeader(HttpRequestBase request, Map<String, String> header) {
if (header != null) {
for (Map.Entry<String, String> entry : header.entrySet()) {
request.setHeader(entry.getKey(), entry.getValue());
}
}
}
示例6: save
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
@Override public HttpResponse save (final IlluminatiEsModel entity) {
final HttpRequestBase httpPutRequest = new HttpPut(entity.getEsUrl(this.esUrl));
if (entity.isSetUserAuth() == true) {
try {
httpPutRequest.setHeader("Authorization", "Basic " + entity.getEsAuthString());
} catch (Exception ex) {
this.logger.error("Sorry. something is wrong in encoding es user auth info. ("+ex.toString()+")");
}
}
((HttpPut) httpPutRequest).setEntity(this.getHttpEntity(entity));
HttpResponse httpResponse = null;
try {
httpResponse = this.httpClient.execute(httpPutRequest);
} catch (IOException e) {
this.logger.error("Sorry. something is wrong in Http Request. ("+e.toString()+")");
} finally {
httpPutRequest.releaseConnection();
}
if (httpResponse == null) {
httpResponse = getHttpResponseByData(this.errorCode, "Sorry. something is wrong in Http Request.");
}
return httpResponse;
}
示例7: setHttpParams
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
public static void setHttpParams(HttpRequestBase httpBase,
int connectMillisTimeout, int readMillisTimeout,
boolean handleRedirects) {
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setConnectTimeout(connectMillisTimeout)
.setSocketTimeout(readMillisTimeout)
.setRedirectsEnabled(handleRedirects).build();
httpBase.setConfig(requestConfig);
httpBase.setHeader("accept-encoding", "gzip");
}
示例8: setAuthorizationHeader
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
public static HttpRequestBase setAuthorizationHeader (ComplianceTestMetaDataHolder complianceTestMetaDataHolder,
HttpRequestBase method) {
String auth = complianceTestMetaDataHolder.getUsername() + ":" + complianceTestMetaDataHolder.getPassword();
if (!auth.equals(":")) {
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
method.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
}
return method;
}
示例9: setHeaders
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
public static void setHeaders(HttpRequestBase method, Headers headers) {
if (headers == null) return;
for (Header h : headers)
method.setHeader(h.getName(), h.getValue());
}
示例10: setDefaultHeader
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
/**
* Adds a default header for HTTP request
* @param request to add header
*/
protected void setDefaultHeader(HttpRequestBase request) {
request.setHeader(CONTENT_TYPE, APPLICATION_JSON);
request.setHeader(CACHE_CONTROL, CACHE_CONTROL_NO_CACHE);
}
示例11: stop
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
private void stop(boolean unregisterAsListener) {
if (cleanupThread != null) {
cleanupThread.dismiss();
cleanupThread = null;
}
setErrorMessage(null);
setRunningState(RunningState.STOPPED);
if (unregisterAsListener) {
try {
GeoEventHttpClient http = httpClientService.createNewClient();
String logouturl = host + "/user/logout";
HttpRequestBase request = HttpUtil.createHttpRequest(http,
logouturl, "POST", "", "application/json",
"application/x-www-form-urlencoded", "", LOGGER);
request.setHeader("Cookie", token);
CloseableHttpResponse response;
try {
response = http.execute(request, httpTimeoutValue);
if (response == null) {
if (getRunningState() == RunningState.ERROR) {
LOGGER.info("RECONNECTION_MSG", clientUrl);
setErrorMessage(null);
setRunningState(RunningState.STARTED);
}
context.setHttpResponse(response);
} else {
// log only if we were not in error state already
if (getRunningState() != RunningState.ERROR) {
;
}
}
} catch (IOException e) {
if( getRunningState() != RunningState.ERROR )
{
String errorMsg = LOGGER.translate("ERROR_ACCESSING_URL", clientUrl, e.getMessage());
LOGGER.error(errorMsg);
LOGGER.info(e.getMessage(), e);
// set the error state
setErrorMessage(errorMsg);
setRunningState(RunningState.ERROR);
}
}
} catch (Throwable t) {
// Chances are we're shutting down...
LOGGER.warn("STOP_ERROR", t.getMessage());
}
}
}
示例12: generateHeaders
import org.apache.http.client.methods.HttpRequestBase; //导入方法依赖的package包/类
/**
* 根据配置自动生成需要的 header
* @param httpMethod
* @param setting
*/
private void generateHeaders(HttpRequestBase httpMethod, Setting setting) {
for(Map.Entry<String,String> headerEntry : setting.getHeader().entrySet()){
httpMethod.setHeader(headerEntry.getKey(),headerEntry.getValue());
}
}