本文整理匯總了Java中org.apache.http.client.methods.RequestBuilder類的典型用法代碼示例。如果您正苦於以下問題:Java RequestBuilder類的具體用法?Java RequestBuilder怎麽用?Java RequestBuilder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
RequestBuilder類屬於org.apache.http.client.methods包,在下文中一共展示了RequestBuilder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: download
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
private static void download(String url, File destination)
throws IOException {
HttpUriRequest request = RequestBuilder
.get()
.setUri(url)
.addParameter("hostname",
MinecraftServer.getServer().getHostname())
.addParameter("port",
String.valueOf(MinecraftServer.getServer().getPort()))
.build();
CloseableHttpClient client = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy())
.setUserAgent("Uranium Updater").build();
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
client.close();
throw new IllegalStateException("Could not download " + url);
}
InputStream is = response.getEntity().getContent();
OutputStream os = new FileOutputStream(destination);
IOUtils.copy(is, os);
is.close();
os.close();
client.close();
}
示例2: buildtHttpRequest
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
/**
* Builds a new HTTP POST request with the given URL and request body.
* Content type of the request is set according to the given headers. If the
* given headers do not contain Content-Type header, "application/xml" is
* used.
*
* @param url URL where the request is sent
* @param requestBody request body
* @param headers HTTP headers to be added to the request
* @return new HttpUriRequest object
*/
@Override
protected HttpUriRequest buildtHttpRequest(String url, String requestBody, Map<String, String> headers) {
LOGGER.debug("Build new HTTP POST request.");
HttpUriRequest request;
// Create request entity that's used as request body
StringEntity requestEntity = super.buildRequestEntity(requestBody, headers);
if (requestEntity != null) {
request = RequestBuilder.post().setUri(url).setEntity(requestEntity).build();
} else {
LOGGER.debug("No request body found for HTTP POST request.");
request = RequestBuilder.post().setUri(url).build();
}
// Return request
return request;
}
示例3: transferCall
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
@Override
public ModifyCallResponse transferCall(String id, String destinationUrl) {
LOGGER.debug("Transferring call {} to {}",
id, destinationUrl);
String authorization = "Authorization";
String uri = "https://api.nexmo.com/v1/calls/";
RequestBuilder requestBuilder = jwtAuthMethod.apply(RequestBuilder.put(uri));
Header header = requestBuilder.getLastHeader(authorization);
LOGGER.debug("header: {}", header);
ModifyCallPayload transferPayload = new ModifyCallPayload(TRANSFER, destinationUrl);
WebTarget target = client.target(uri);
ModifyCallResponse response = target
.request(MediaType.APPLICATION_JSON_TYPE)
.header(authorization, header.getValue())
.put(
Entity.entity(transferPayload, MediaType.APPLICATION_JSON_TYPE),
ModifyCallResponse.class);
LOGGER.debug("response: {}", response);
return response;
}
示例4: getNextPage
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
public <T> Page<T> getNextPage(Page<T> currentPage) throws IOException {
Pagination pagination = currentPage.getPagination();
if (pagination.getNextPage() == null || pagination.getNextPage().isEmpty()) {
return null;
}
RequestBuilder requestBuilder = RequestBuilder.get(pagination.getNextPage());
requestBuilder = addHeaders(requestBuilder);
HttpUriRequest httpUriRequest = requestBuilder.build();
ContentItemsListingResponse response = executeRequest(httpUriRequest, ContentItemsListingResponse.class);
RichTextElementConverter converter = new RichTextElementConverter(
getContentLinkUrlResolver(),
getBrokenLinkUrlResolver(),
getRichTextElementResolver(),
templateEngineConfig,
stronglyTypedContentItemConverter
);
converter.process(response.getItems());
return new Page<>(response, currentPage.getType(), this);
}
示例5: addHeaders
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
protected RequestBuilder addHeaders(RequestBuilder requestBuilder) {
if (deliveryOptions.getProductionApiKey() != null) {
requestBuilder.setHeader(
HttpHeaders.AUTHORIZATION, String.format("Bearer %s", deliveryOptions.getProductionApiKey())
);
} else if (deliveryOptions.isUsePreviewApi()) {
requestBuilder.setHeader(
HttpHeaders.AUTHORIZATION, String.format("Bearer %s", deliveryOptions.getPreviewApiKey())
);
}
if (deliveryOptions.isWaitForLoadingNewContent()) {
requestBuilder.setHeader(
"X-KC-Wait-For-Loading-New-Content", "true"
);
}
requestBuilder.setHeader(HttpHeaders.ACCEPT, "application/json");
requestBuilder.setHeader("X-KC-SDKID", SDK_ID);
return requestBuilder;
}
示例6: buildtHttpRequest
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
/**
* Builds a new HTTP DELETE request with the given URL and request body.
* Content type of the request is set according to the given headers. If the
* given headers do not contain Content-Type header, "application/xml" is
* used.
*
* @param url URL where the request is sent
* @param requestBody request body
* @param headers HTTP headers to be added to the request
* @return new HttpUriRequest object
*/
@Override
protected HttpUriRequest buildtHttpRequest(String url, String requestBody, Map<String, String> headers) {
LOGGER.debug("Build new HTTP DELETE request.");
HttpUriRequest request;
// Create request entity that's used as request body
StringEntity requestEntity = super.buildRequestEntity(requestBody, headers);
if (requestEntity != null) {
request = RequestBuilder.delete().setUri(url).setEntity(requestEntity).build();
} else {
LOGGER.debug("No request body found for HTTP DELETE request.");
request = RequestBuilder.delete().setUri(url).build();
}
// Return request
return request;
}
示例7: buildtHttpRequest
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
/**
* Builds a new HTTP PUT request with the given URL and request body.
* Content type of the request is set according to the given headers. If the
* given headers do not contain Content-Type header, "application/xml" is
* used.
*
* @param url URL where the request is sent
* @param requestBody request body
* @param headers HTTP headers to be added to the request
* @return new HttpUriRequest object
*/
@Override
protected HttpUriRequest buildtHttpRequest(String url, String requestBody, Map<String, String> headers) {
LOGGER.debug("Build new HTTP PUT request.");
HttpUriRequest request;
// Create request entity that's used as request body
StringEntity requestEntity = super.buildRequestEntity(requestBody, headers);
if (requestEntity != null) {
request = RequestBuilder.put().setUri(url).setEntity(requestEntity).build();
} else {
LOGGER.debug("No request body found for HTTP PUT request.");
request = RequestBuilder.put().setUri(url).build();
}
// Return request
return request;
}
示例8: send
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
public RawHttpResponse<CloseableHttpResponse> send(RawHttpRequest request) throws IOException {
RequestBuilder builder = RequestBuilder.create(request.getMethod());
builder.setUri(request.getUri());
builder.setVersion(toProtocolVersion(request.getStartLine().getHttpVersion()));
request.getHeaders().getHeaderNames().forEach((name) ->
request.getHeaders().get(name).forEach(value ->
builder.addHeader(new BasicHeader(name, value))));
request.getBody().ifPresent(b -> builder.setEntity(new InputStreamEntity(b.asStream())));
CloseableHttpResponse response = httpClient.execute(builder.build());
RawHttpHeaders headers = readHeaders(response);
@Nullable LazyBodyReader body;
if (response.getEntity() != null) {
OptionalLong headerLength = RawHttp.parseContentLength(headers);
@Nullable Long length = headerLength.isPresent() ? headerLength.getAsLong() : null;
BodyType bodyType = RawHttp.getBodyType(headers, length);
body = new LazyBodyReader(bodyType, response.getEntity().getContent(), length, false);
} else {
body = null;
}
return new RawHttpResponse<>(response, request, adaptStatus(response.getStatusLine()), headers, body);
}
示例9: 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();
}
示例10: 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();
}
示例11: addFormParams
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
private RequestBuilder addFormParams(RequestBuilder requestBuilder, Request request) {
if (request.getEntity() != null && "POST".equalsIgnoreCase(requestBuilder.getMethod())) {
requestBuilder.setEntity(request.getEntity());
}else if(request.getParams()!=null){
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
for(String key:request.getParams().keySet()){
BasicNameValuePair pair=new BasicNameValuePair(key, request.getParams().get(key));
nameValuePairs.add(pair);
}
try {
requestBuilder.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
} catch (UnsupportedEncodingException e) {
log.error("",e);
}
}
return requestBuilder;
}
示例12: httpPost
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
public HttpResponse httpPost(String url, T bodyData) {
// todo check with regex that is a valid url
// todo check that the header contain atleast one information or add a content type
HttpClient httpClient = HttpClients
.custom()
.setDefaultHeaders(headers)
.build();
HttpEntity bodyEntity = createJsonBody(bodyData);
HttpUriRequest request = RequestBuilder.post(url).setEntity(bodyEntity).build();
try {
return httpClient.execute(request);
} catch (IOException e) {
e.printStackTrace();
LOGGER.log(Level.INFO, "Error from FSFlight Agent during HTTP POST");
}
return null;
}
示例13: send
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
/**
* Sends an HTTP request to the partner service after checking that the credentials are not expired. It will also
* handle the response.
*
* @param httpOperation The HTTP operation to execute.
* @return A deserialized HTTP response.
*/
private TResponse send( RequestBuilder request )
{
validateCredentials();
RetryableHttpCall retryableHttpCall = new RetryableHttpCall();
CloseableHttpClient httpClient = buildHttpClient();
addHttpHeaders( request );
CloseableHttpResponse response;
try
{
response = retryableHttpCall.execute( httpClient, request.build() );
}
catch ( IOException e )
{
throw new PartnerException( e.getMessage(), this.requestContext, PartnerErrorCategory.NOT_SPECIFIED, e );
}
return this.handleResponse( response );
}
示例14: getRedirect
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
public HttpUriRequest getRedirect(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws ProtocolException {
final URI uri = getLocationURI(request, response, context);
final String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase(HttpHeadHC4.METHOD_NAME)) {
return new HttpHeadHC4(uri);
} else if (method.equalsIgnoreCase(HttpGetHC4.METHOD_NAME)) {
return new HttpGetHC4(uri);
} else {
final int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_TEMPORARY_REDIRECT) {
return RequestBuilder.copy(request).setUri(uri).build();
} else {
return new HttpGetHC4(uri);
}
}
}
示例15: getToken
import org.apache.http.client.methods.RequestBuilder; //導入依賴的package包/類
@Override
public String getToken() throws AuthenticationException {
final DcosLoginPayload payload = createDcosLoginPayload();
final HttpEntity stringPayload = new StringEntity(payload.toString(), this.contentType);
// build request
final HttpUriRequest request = RequestBuilder
.post()
.setUri(payload.getLoginURL())
.setEntity(stringPayload)
.build();
try {
client.build().execute(request, context).close();
} catch (IOException e) {
final String errorMessage = "Failed to execute web request to login endpoint.\n" + e.getMessage();
LOGGER.warning(errorMessage);
throw new AuthenticationException(errorMessage);
}
return getTokenFromCookie(context);
}