本文整理汇总了Java中org.apache.http.client.fluent.Request.addHeader方法的典型用法代码示例。如果您正苦于以下问题:Java Request.addHeader方法的具体用法?Java Request.addHeader怎么用?Java Request.addHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.fluent.Request
的用法示例。
在下文中一共展示了Request.addHeader方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: put
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
/**
* Send PUT request with authorization header
* @param url - The url of the POST request
* @param auth - String for authorization header
* @param putData - The body of the PUT
*/
public Response put(String url, String auth, JsonJavaObject putData) throws URISyntaxException, IOException, JsonException {
URI normUri = new URI(url).normalize();
Request putRequest = Request.Put(normUri);
//Add auth header
if(StringUtil.isNotEmpty(auth)) {
putRequest.addHeader("Authorization", auth);
}
//Add put data
String putDataString = JsonGenerator.toJson(JsonJavaFactory.instanceEx, putData);
if(putData != null) {
putRequest = putRequest.bodyString(putDataString, ContentType.APPLICATION_JSON);
}
Response response = executor.execute(putRequest);
return response;
}
示例2: addHeadersToRequest
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
@VisibleForTesting
void addHeadersToRequest(final Request request, final URL trackerAnnounceURI) {
final int port = trackerAnnounceURI.getPort();
// Add Host header first to prevent Request appending it at the end
request.addHeader("Host", trackerAnnounceURI.getHost() + (port == -1 ? "" : ":" + port));
//noinspection SimplifyStreamApiCallChains
this.headers.stream().forEachOrdered(header -> {
final String name = header.getKey();
final String value = header.getValue()
.replaceAll("\\{java}", System.getProperty("java.version"))
.replaceAll("\\{os}", System.getProperty("os.name"))
.replaceAll("\\{locale}", Locale.getDefault().toLanguageTag());
request.addHeader(name, value);
});
// if Connection header was not set, we append it. Apache HttpClient will add it what so ever, so prefer "Close" over "keep-alive"
final boolean hasConnectionHeader = this.headers.stream()
.anyMatch(header -> "Connection".equalsIgnoreCase(header.getKey()));
if (!hasConnectionHeader) {
request.addHeader("Connection", "Close");
}
}
示例3: post
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
/**
* Send POST request with authorization header and additional headers
* @param url - The url of the POST request
* @param auth - String for authorization header
* @param headers - Hashmap of headers to add to the request
* @param postData - The body of the POST
* @return the Response to the POST request
*/
public Response post(String url, String auth, HashMap<String, String> headers, JsonJavaObject postData) throws JsonException, IOException, URISyntaxException {
URI normUri = new URI(url).normalize();
Request postRequest = Request.Post(normUri);
//Add all headers
if(StringUtil.isNotEmpty(auth)) {
postRequest.addHeader("Authorization", auth);
}
if(headers != null && headers.size() > 0){
for (Map.Entry<String, String> entry : headers.entrySet()) {
postRequest.addHeader(entry.getKey(), entry.getValue());
}
}
String postDataString = JsonGenerator.toJson(JsonJavaFactory.instanceEx, postData);
Response response = executor.execute(postRequest.bodyString(postDataString, ContentType.APPLICATION_JSON));
return response;
}
示例4: prepareRequest
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
private void prepareRequest(Request apacheRequest) {
if (getUserAgent() != null) {
apacheRequest.userAgent(getUserAgent());
}
if (getConnectTimeout() != null) {
apacheRequest.connectTimeout(getConnectTimeout());
}
if (getReadTimeout() != null) {
apacheRequest.socketTimeout(getReadTimeout());
}
if (!requestHeaders.isEmpty()) {
for (Map.Entry<String, String> entry : requestHeaders.entrySet()) {
apacheRequest.addHeader(entry.getKey(), entry.getValue());
}
}
}
示例5: getToken
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
public static String getToken(String code){
try{
Request request = Request.Post("https://discordapp.com/api/oauth2/token"
+"?grant_type=authorization_code"
+"&code="+code
+"&redirect_uri="+URLEncoder.encode(Pokebot.config.REDIRECT_URL, "UTF-8")
+"&client_id="+Pokebot.config.CLIENT_ID
+"&client_secret="+Pokebot.config.CLIENT_SECRET);
request.addHeader("Content-Type", "application/x-www-form-urlencoded");
TokenResponse tokenResponse = gson.fromJson(request.execute().returnContent().asString(), TokenResponse.class);
if(tokenResponse.access_token == null){
System.err.println(tokenResponse.error);
return null;
}
return tokenResponse.access_token;
} catch(IOException e){
e.printStackTrace();
System.err.println("\nUnable to get token");
}
return null;
}
示例6: addRestComm
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private static void addRestComm(Request request, Map<String, Object> args) {
request.connectTimeout(connectTimeout);
request.userAgent("Mozilla");
request.socketTimeout(socketTimeout);
if (args.containsKey("header")) {
Object object = args.get("header");
if (object instanceof Map) {
Map<String, String> mapHeader = (Map<String, String>) object;
for (String o : mapHeader.keySet()) {
request.addHeader(o, mapHeader.get(o));
}
}
}
}
示例7: applyRequestHeaders
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
void applyRequestHeaders(final Request request, final Map<String, String> requestHeaders) {
if (requestHeaders == null)
return;
for (Map.Entry<String, String> h : requestHeaders.entrySet()) {
request.addHeader(h.getKey(), h.getValue());
}
}
示例8: get
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
/**
* Send basic GET request with authorization header
*/
public Response get(String url, String auth) throws URISyntaxException, IOException {
URI normUri = new URI(url).normalize();
Request getRequest = Request.Get(normUri);
if(StringUtil.isNotEmpty(auth)) {
getRequest.addHeader("Authorization", auth);
}
Response response = executor.execute(getRequest);
return response;
}
示例9: downloader
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
/**
* 下载,并返回string
* @throws IOException
* @throws ClientProtocolException
**/
public String downloader(String url) throws ClientProtocolException, IOException {
String res = "";
Request rq = Request.Get(url).connectTimeout(this.timeout);
if (this.agent != null)
rq = rq.userAgent(this.agent);
if (this.cookie != null)
rq = rq.addHeader("Cookie", this.cookie);
if (this.proxy != null)
rq = rq.viaProxy(this.proxy);
res = rq.execute().returnContent().asString();
return res;
}
示例10: httpGet
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
protected String httpGet(String url, String encoding,
Map<String, String> headers) throws IOException, CredentialInvalidException {
Request request = Request.Get(url).connectTimeout(15000)
.socketTimeout(15000);
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
}
return executeRequest(encoding, request);
}
示例11: httpPost
import org.apache.http.client.fluent.Request; //导入方法依赖的package包/类
protected String httpPost(String url, String encoding,
List<NameValuePair> formParams, Map<String, String> headers)
throws IOException, CredentialInvalidException {
Request request = Request.Post(url).bodyForm(formParams)
.connectTimeout(15000).socketTimeout(15000);
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
request.addHeader(entry.getKey(), entry.getValue());
}
}
return executeRequest(encoding, request);
}