本文整理匯總了Java中org.springframework.http.client.ClientHttpRequest.execute方法的典型用法代碼示例。如果您正苦於以下問題:Java ClientHttpRequest.execute方法的具體用法?Java ClientHttpRequest.execute怎麽用?Java ClientHttpRequest.execute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.http.client.ClientHttpRequest
的用法示例。
在下文中一共展示了ClientHttpRequest.execute方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doTest
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
private void doTest(AnnotationConfigEmbeddedWebApplicationContext context,
String resourcePath) throws Exception {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = clientHttpRequestFactory.createRequest(
new URI("http://localhost:"
+ context.getEmbeddedServletContainer().getPort() + resourcePath),
HttpMethod.GET);
ClientHttpResponse response = request.execute();
try {
String actual = StreamUtils.copyToString(response.getBody(),
Charset.forName("UTF-8"));
assertThat(actual).isEqualTo("Hello World");
}
finally {
response.close();
}
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:18,代碼來源:EmbeddedServletContainerMvcIntegrationTests.java
示例2: onApplicationEvent
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
@Override
public void onApplicationEvent(ClassPathChangedEvent event) {
try {
ClassLoaderFiles classLoaderFiles = getClassLoaderFiles(event);
ClientHttpRequest request = this.requestFactory.createRequest(this.uri,
HttpMethod.POST);
byte[] bytes = serialize(classLoaderFiles);
HttpHeaders headers = request.getHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentLength(bytes.length);
FileCopyUtils.copy(bytes, request.getBody());
logUpload(classLoaderFiles);
ClientHttpResponse response = request.execute();
Assert.state(response.getStatusCode() == HttpStatus.OK, "Unexpected "
+ response.getStatusCode() + " response uploading class files");
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
示例3: download
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
public byte[] download(String url){
for (int retryCount = 0; retryCount < maxRetryCount; retryCount++){
try{
ClientHttpRequest request = createRequest(url);
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (ClientHttpResponse response = request.execute()){
responseRefs.add(new WeakReference<>(response, referenceQueue));
InputStream input = response.getBody();
IOUtils.copy(input, output);
}
return output.toByteArray();
}catch (IOException ex){
if (ex instanceof SocketException && closed){
return null;
}
logger.info("IO異常,重試", ex);
}
}
logger.info("到達最大重試次數,已放棄重試");
return null;
}
示例4: doTest
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
private void doTest(AnnotationConfigEmbeddedWebApplicationContext context,
String resourcePath) throws Exception {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = clientHttpRequestFactory.createRequest(
new URI("http://localhost:"
+ context.getEmbeddedServletContainer().getPort() + resourcePath),
HttpMethod.GET);
ClientHttpResponse response = request.execute();
try {
String actual = StreamUtils.copyToString(response.getBody(),
Charset.forName("UTF-8"));
assertThat(actual, equalTo("Hello World"));
}
finally {
response.close();
}
}
示例5: userInfoLoadBalancedNoRetry
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
@Test
public void userInfoLoadBalancedNoRetry() throws Exception {
this.context = new SpringApplicationBuilder(ClientConfiguration.class)
.properties("spring.config.name=test", "server.port=0",
"security.oauth2.resource.userInfoUri:http://nosuchservice",
"security.oauth2.resource.loadBalanced=true")
.run();
assertTrue(this.context.containsBean("loadBalancedUserInfoRestTemplateCustomizer"));
assertFalse(this.context.containsBean("retryLoadBalancedUserInfoRestTemplateCustomizer"));
OAuth2RestTemplate template = this.context
.getBean(UserInfoRestTemplateFactory.class).getUserInfoRestTemplate();
ClientHttpRequest request = template.getRequestFactory()
.createRequest(new URI("http://nosuchservice"), HttpMethod.GET);
expected.expectMessage("No instances available for nosuchservice");
request.execute();
}
開發者ID:spring-cloud,項目名稱:spring-cloud-security,代碼行數:19,代碼來源:OAuth2LoadBalancerClientAutoConfigurationTests.java
示例6: doExecute
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
/**
* Execute the given method on the provided URI.
* <p>The {@link ClientHttpRequest} is processed using the {@link RequestCallback};
* the response with the {@link ResponseExtractor}.
* @param url the fully-expanded URL to connect to
* @param method the HTTP method to execute (GET, POST, etc.)
* @param requestCallback object that prepares the request (can be {@code null})
* @param responseExtractor object that extracts the return value from the response (can be {@code null})
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
ClientHttpResponse response = null;
try {
ClientHttpRequest request = createRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
response = request.execute();
if (!getErrorHandler().hasError(response)) {
logResponseStatus(method, url, response);
}
else {
handleResponseError(method, url, response);
}
if (responseExtractor != null) {
return responseExtractor.extractData(response);
}
else {
return null;
}
}
catch (IOException ex) {
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + url + "\":" + ex.getMessage(), ex);
}
finally {
if (response != null) {
response.close();
}
}
}
示例7: doExecute
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
/**
* Execute the given method on the provided URI.
* <p>The {@link ClientHttpRequest} is processed using the {@link RequestCallback};
* the response with the {@link ResponseExtractor}.
* @param url the fully-expanded URL to connect to
* @param method the HTTP method to execute (GET, POST, etc.)
* @param requestCallback object that prepares the request (can be {@code null})
* @param responseExtractor object that extracts the return value from the response (can be {@code null})
* @return an arbitrary object, as returned by the {@link ResponseExtractor}
*/
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url, "'url' must not be null");
Assert.notNull(method, "'method' must not be null");
ClientHttpResponse response = null;
try {
ClientHttpRequest request = createRequest(url, method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
response = request.execute();
handleResponse(url, method, response);
if (responseExtractor != null) {
return responseExtractor.extractData(response);
}
else {
return null;
}
}
catch (IOException ex) {
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + url + "\": " + ex.getMessage(), ex);
}
finally {
if (response != null) {
response.close();
}
}
}
示例8: getClientResponse
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
protected ClientHttpResponse getClientResponse(String url, HttpMethod method,
HttpComponentsClientHttpRequestFactory requestFactory, String... headers)
throws IOException, URISyntaxException {
ClientHttpRequest request = requestFactory.createRequest(new URI(url), method);
request.getHeaders().add("Cookie", "JSESSIONID=" + "123");
for (String header : headers) {
String[] parts = header.split(":");
request.getHeaders().add(parts[0], parts[1]);
}
ClientHttpResponse response = request.execute();
return response;
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:13,代碼來源:AbstractEmbeddedServletContainerFactoryTests.java
示例9: hasHeader
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
public boolean hasHeader(String url, int port, String header) throws Exception {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = clientHttpRequestFactory
.createRequest(new URI("http://localhost:" + port + url), HttpMethod.GET);
ClientHttpResponse response = request.execute();
return response.getHeaders().containsKey(header);
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:8,代碼來源:EndpointWebMvcAutoConfigurationTests.java
示例10: verify404
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
private void verify404() throws Exception {
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
ClientHttpRequest request = requestFactory.createRequest(
new URI("http://localhost:"
+ this.context.getEmbeddedServletContainer().getPort() + "/"),
HttpMethod.GET);
ClientHttpResponse response = request.execute();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:10,代碼來源:MultipartAutoConfigurationTests.java
示例11: isUp
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
private boolean isUp() {
try {
ClientHttpRequest request = createRequest();
ClientHttpResponse response = request.execute();
return response.getStatusCode() == HttpStatus.OK;
}
catch (Exception ex) {
return false;
}
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:11,代碼來源:DelayedLiveReloadTrigger.java
示例12: assertContent
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
public void assertContent(String url, int port, Object expected) throws Exception {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = clientHttpRequestFactory
.createRequest(new URI("http://localhost:" + port + url), HttpMethod.GET);
try {
ClientHttpResponse response = request.execute();
if (HttpStatus.NOT_FOUND.equals(response.getStatusCode())) {
throw new FileNotFoundException();
}
try {
String actual = StreamUtils.copyToString(response.getBody(),
Charset.forName("UTF-8"));
if (expected instanceof Matcher) {
assertThat(actual).is(Matched.by((Matcher<?>) expected));
}
else {
assertThat(actual).isEqualTo(expected);
}
}
finally {
response.close();
}
}
catch (Exception ex) {
if (expected == null) {
if (SocketException.class.isInstance(ex)
|| FileNotFoundException.class.isInstance(ex)) {
return;
}
}
throw ex;
}
}
示例13: getJson
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
private static String getJson(RestTemplate restTemplate, String urlString) {
ClientHttpResponse response = null;
HttpMethod method = null;
try {
method = HttpMethod.GET;
URI url = new UriTemplate(urlString).expand();
ClientHttpRequest request = restTemplate.getRequestFactory().createRequest(url, method);
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
request.getHeaders().setAccept(acceptableMediaTypes );
//if (requestCallback != null) {
// requestCallback.doWithRequest(request);
//}
response = request.execute();
if (response.getBody() != null) {
HttpMessageConverterExtractor<String> extractor = new HttpMessageConverterExtractor<String>(String.class, restTemplate.getMessageConverters());
String data = extractor.extractData(response);
return data;
};
}
catch (IOException ex) {
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + urlString + "\":" + ex.getMessage(), ex);
}
finally {
if (response != null) {
response.close();
}
}
return null;
}
示例14: getClientResponse
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
protected ClientHttpResponse getClientResponse(String url,
HttpComponentsClientHttpRequestFactory requestFactory, String... headers)
throws IOException, URISyntaxException {
ClientHttpRequest request = requestFactory.createRequest(new URI(url),
HttpMethod.GET);
request.getHeaders().add("Cookie", "JSESSIONID=" + "123");
for (String header : headers) {
String[] parts = header.split(":");
request.getHeaders().add(parts[0], parts[1]);
}
ClientHttpResponse response = request.execute();
return response;
}
示例15: assertContent
import org.springframework.http.client.ClientHttpRequest; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
public void assertContent(String url, int port, Object expected) throws Exception {
SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory();
ClientHttpRequest request = clientHttpRequestFactory
.createRequest(new URI("http://localhost:" + port + url), HttpMethod.GET);
try {
ClientHttpResponse response = request.execute();
try {
String actual = StreamUtils.copyToString(response.getBody(),
Charset.forName("UTF-8"));
if (expected instanceof Matcher) {
assertThat(actual, is((Matcher<String>) expected));
}
else {
assertThat(actual, equalTo(expected));
}
}
finally {
response.close();
}
}
catch (Exception ex) {
if (expected == null) {
if (SocketException.class.isInstance(ex)
|| FileNotFoundException.class.isInstance(ex)) {
return;
}
}
throw ex;
}
}