本文整理匯總了Java中org.apache.http.client.methods.RequestBuilder.addHeader方法的典型用法代碼示例。如果您正苦於以下問題:Java RequestBuilder.addHeader方法的具體用法?Java RequestBuilder.addHeader怎麽用?Java RequestBuilder.addHeader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.methods.RequestBuilder
的用法示例。
在下文中一共展示了RequestBuilder.addHeader方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: post0
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
private static HttpUriRequest post0(String url, Map<String, Object> params) {
RequestBuilder requestBuilder = RequestBuilder.post().setCharset(CHARSET).setUri(url);
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setConnectionRequestTimeout(10000).setSocketTimeout(10000).setConnectTimeout(10000).setCookieSpec("ignoreCookies");
requestBuilder.setConfig(requestConfigBuilder.build());
if (params != null && params.size() > 0) {
List<NameValuePair> nvList = new ArrayList();
Iterator var6 = params.entrySet().iterator();
while (var6.hasNext()) {
Map.Entry<String, Object> param = (Map.Entry) var6.next();
Object value = param.getValue();
if (value != null) {
nvList.add(new BasicNameValuePair((String) param.getKey(), value.toString()));
}
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvList, CHARSET);
requestBuilder.setEntity(formEntity);
}
requestBuilder.addHeader("Connection", "Keep-Alive");
return requestBuilder.build();
}
示例2: post0
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
private static HttpUriRequest post0(String url, Map<String, String> params) {
RequestBuilder requestBuilder = RequestBuilder.post().setCharset(CHARSET).setUri(url);
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setConnectionRequestTimeout(TIMEOUT)
.setSocketTimeout(TIMEOUT)
.setConnectTimeout(TIMEOUT).setCookieSpec(CookieSpecs.IGNORE_COOKIES);
requestBuilder.setConfig(requestConfigBuilder.build());
if (params != null && params.size() > 0) {
List<NameValuePair> nvList = new ArrayList<>();
for (Map.Entry<String, String> param : params.entrySet()) {
Object value = param.getValue();
if (value == null) {
continue;
}
nvList.add(new BasicNameValuePair(param.getKey(), value.toString()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvList, CHARSET);
requestBuilder.setEntity(formEntity);
}
requestBuilder.addHeader("Connection", "Keep-Alive");
return requestBuilder.build();
}
示例3: buildHttpUriRequest
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
protected HttpUriRequest buildHttpUriRequest(RequestBuilder builder) {
RequestBuilder requestBuilder = builder.setUri(this.address);
if (this.param != null && !this.param.isEmpty()) {
for (Map.Entry<String, String> entry : this.param.entrySet()) {
requestBuilder.addParameter(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
if (this.header != null) {
for (String name : this.header) {
requestBuilder.addHeader(name, this.header.getHeader(name));
}
}
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(30000)
.setConnectTimeout(this.connectTimeout)
.setConnectionRequestTimeout(this.readTimeout)
.build();
requestBuilder.setConfig(requestConfig);
return requestBuilder.build();
}
示例4: makeRequest
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
/**
* Creates a {@link org.apache.http.HttpRequest} given a type, path, and optional content.
*
* @param requestType The type of HTTP/1.1 request to make (see {@link RequestType}
* for more details.
* @param uri The URI to request content from.
* @param args The content of the request. This parameter is optional and considered to be nullable.
*
* @return A request built from the specification above.
*/
private HttpUriRequest makeRequest(RequestType requestType, URI uri, Object... args) {
RequestConfig.Builder config = RequestConfig.copy(RequestConfig.DEFAULT);
config.setCookieSpec(CookieSpecs.BEST_MATCH);
RequestBuilder requestBuilder = RequestBuilder.create(requestType.name())
.setUri(uri)
.setConfig(config.build())
.setEntity(this.makeEntity(args))
.setHeader("User-Agent", this.getUserAgent());
if (this.oauthToken != null) {
requestBuilder.addHeader("Authorization", "Bearer " + this.oauthToken);
}
if (this.jwt != null) {
requestBuilder.addHeader("Authorization", "JWT " + this.jwtString);
}
if (this.csrfToken != null) {
requestBuilder.addHeader(CSRF_TOKEN_HEADER, this.csrfToken);
}
return requestBuilder.build();
}
示例5: executeServerQueryRaw
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
private <T> ServerQueryResponse<T> executeServerQueryRaw(
@Nullable final QueryOptions<T> options,
@Nullable final String wait,
final ValueExtractor<T> valueExtractor,
RequestBuilder requestBuilder
) throws IOException, NomadException {
String region = apiClient.getConfig().getRegion();
String namespace = apiClient.getConfig().getNamespace();
String authToken = apiClient.getConfig().getAuthToken();
if (options != null) {
if (options.getRegion() != null)
region = options.getRegion();
if (options.getNamespace() != null)
namespace = options.getNamespace();
if (options.getAuthToken() != null)
authToken = options.getAuthToken();
if (options.getIndex() != null)
requestBuilder.addParameter("index", options.getIndex().toString());
if (wait != null)
requestBuilder.addParameter("wait", wait);
if (options.isAllowStale())
requestBuilder.addParameter("stale", null);
}
if (region != null)
requestBuilder.addParameter("region", region);
if (namespace != null)
requestBuilder.addParameter("namespace", namespace);
if (authToken != null)
requestBuilder.addHeader("X-Nomad-Token", authToken);
return apiClient.execute(requestBuilder, new ServerQueryResponseAdapter<>(valueExtractor));
}
示例6: prepareWrite
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
private RequestBuilder prepareWrite(
final RequestBuilder builder,
final URIBuilder uri,
@Nullable final WriteOptions options
) {
String region = apiClient.getConfig().getRegion();
String namespace = apiClient.getConfig().getNamespace();
String secretId = apiClient.getConfig().getAuthToken();
if (options != null) {
if (options.getRegion() != null)
region = options.getRegion();
if (options.getNamespace() != null)
namespace = options.getNamespace();
if (options.getSecretId() != null)
secretId = options.getSecretId();
}
if (region != null)
uri.addParameter("region", region);
if (namespace != null)
uri.addParameter("namespace", namespace);
if (secretId != null)
builder.addHeader("X-Nomad-Token", secretId);
return builder.setUri(build(uri));
}
示例7: privateRequest
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
private ApiResponse privateRequest(String httpMethod, String endpoint, String body, HashMap<String, String> query, int requestcount) {
if (!ApiSettings.isValid())
throw new IllegalStateException("ApiSettings have an invalid state");
String endpointFinal = ApiSettings.getApiEndpoint() + endpoint;
try {
StringEntity entity = new StringEntity(body, ContentType.APPLICATION_JSON);
RequestBuilder requestBuilder = RequestBuilder.create(httpMethod);
requestBuilder.addHeader("Voat-ApiKey", ApiSettings.getPublicKey());
requestBuilder.setUri(endpointFinal);
if (!httpMethod.equals("GET"))
requestBuilder.setEntity(entity);
if (query != null)
for (String key : query.keySet()) {
requestBuilder.addParameter(key, query.get(key));
}
HttpUriRequest build = requestBuilder.build();
Authenticator.getInstance().authenticateRequest(build);
String s = HttpHelper.sendRequest(build);
HashMap<String, Object> json = JsonHelper.getInstance().fromJson(s);
return new ApiResponse(json);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例8: login
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
public void login(String username, String password) throws IOException, ParseException {
RequestBuilder builder = RequestBuilder.create("POST");
StringEntity entity = new StringEntity(String.format("grant_type=password&username=%s&password=%s", username, password), ContentType.APPLICATION_FORM_URLENCODED);
builder.setEntity(entity);
builder.addHeader("Voat-ApiKey", ApiSettings.getPublicKey());
builder.setUri(ApiSettings.getApiEndpoint() + "api/token");
String response = HttpHelper.sendRequest(builder.build());
HashMap<String, Object> map = JsonHelper.getInstance().fromJson(response);
accessToken = (String) map.get("access_token");
}
示例9: addCommonHeaders
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
private void addCommonHeaders(RequestBuilder requestBuilder) {
requestBuilder.addHeader("x-ms-version", "2014-02-01");
requestBuilder.addHeader("Accept", "application/json");
}
示例10: authenticateRequest
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
public void authenticateRequest(RequestBuilder request) {
request.addHeader("Authorization", String.format("Bearer %s", accessToken));
}
示例11: toHttpUriRequest
import org.apache.http.client.methods.RequestBuilder; //導入方法依賴的package包/類
HttpUriRequest toHttpUriRequest(Request request, Request.Options options) throws
UnsupportedEncodingException, MalformedURLException, URISyntaxException {
RequestBuilder requestBuilder = RequestBuilder.create(request.method());
//per request timeouts
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectTimeout(options.connectTimeoutMillis())
.setSocketTimeout(options.readTimeoutMillis())
.build();
requestBuilder.setConfig(requestConfig);
URI uri = new URIBuilder(request.url()).build();
requestBuilder.setUri(uri.getScheme() + "://" + uri.getAuthority() + uri.getRawPath());
//request query params
List<NameValuePair> queryParams = URLEncodedUtils.parse(uri, requestBuilder.getCharset().name
());
for (NameValuePair queryParam : queryParams) {
requestBuilder.addParameter(queryParam);
}
//request headers
boolean hasAcceptHeader = false;
for (Map.Entry<String, Collection<String>> headerEntry : request.headers().entrySet()) {
String headerName = headerEntry.getKey();
if (headerName.equalsIgnoreCase(ACCEPT_HEADER_NAME)) {
hasAcceptHeader = true;
}
if (headerName.equalsIgnoreCase(Util.CONTENT_LENGTH)) {
// The 'Content-Length' header is always set by the Apache client and it
// doesn't like us to set it as well.
continue;
}
for (String headerValue : headerEntry.getValue()) {
requestBuilder.addHeader(headerName, headerValue);
}
}
//some servers choke on the default accept string, so we'll set it to anything
if (!hasAcceptHeader) {
requestBuilder.addHeader(ACCEPT_HEADER_NAME, "*/*");
}
//request body
if (request.body() != null) {
HttpEntity entity = null;
if (request.charset() != null) {
ContentType contentType = getContentType(request);
String content = new String(request.body(), request.charset());
entity = new StringEntity(content, contentType);
} else {
entity = new ByteArrayEntity(request.body());
}
requestBuilder.setEntity(entity);
}
return requestBuilder.build();
}