spring 使用RestTemplate时出现异常,消息为“http协议不受支持”

mkh04yzy  于 2023-09-29  发布在  Spring
关注(0)|答案(3)|浏览(168)

我尝试使用RestTemplate通过GET请求命中一个URL。它给出了一个异常,说明不支持http协议。
我能够使用普通的HttpURLConnection方法来访问端点,从而获得预期的响应。但我无法使用rest模板来完成此操作。我没有使用任何类型的VPN或代理,而尝试这个。
下面是我使用的代码。我将用ip:port替换实际使用的ip和port。

final String url = "http://ip:port/h2h.php?channelid=acrs&posid=1";
final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_XML);
httpHeaders.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_VALUE);
final HttpEntity<String> httpEntity = new HttpEntity<>("", httpHeaders)
restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);

异常跟踪

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://ip:port/h2h.php": http protocol is not supported; nested exception is org.apache.http.conn.UnsupportedSchemeException: http protocol is not supported
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:673) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:538) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.client.RestTemplate$$FastClassBySpringCGLIB$$aa4e9ed0.invoke(<generated>) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.cloud.netflix.metrics.RestTemplateUrlTemplateCapturingAspect.captureUrlTemplate(RestTemplateUrlTemplateCapturingAspect.java:33) ~[spring-cloud-netflix-core-1.4.3.RELEASE.jar:1.4.3.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at org.springframework.web.client.RestTemplate$$EnhancerBySpringCGLIB$$8fab6610.exchange(<generated>) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    at com.gdn.x.pulsa.service.impl.TransactionServiceImpl.getTransactionResult(MobileTransactionServiceImpl.java:118) ~[classes/:na]

任何帮助将不胜感激。
谢谢
编辑:使用HttpUrlConnection的代码

try {
      URL obj = new URL(url);
      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
      con.setRequestMethod("GET");
      con.setRequestProperty("User-Agent", "Mozilla/5.0");
      int responseCode = con.getResponseCode();
      System.out.println("\nSending 'GET' request to URL : " + url);
      System.out.println("Response Code : " + responseCode);
      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();
      while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
      }
      in.close();
      System.out.println(response.toString());
    } catch (Exception e) {

    }
hs1rzwqc

hs1rzwqc1#

try using a closebale httpclient 
   CloseableHttpClient httpClient = HttpClients.custom()
            .setSSLHostnameVerifier(new NoopHostnameVerifier())
            .build();
    HttpComponentsClientHttpRequestFactory requestFactory
            = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(httpClient);

    ResponseEntity<String> response
            = new RestTemplate(requestFactory).exchange(
            url, HttpMethod.GET, httpEntity, String.class);
vh0rcniy

vh0rcniy2#

以下是RestTemplate的用法:

String payload = "something";
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForObject(url, new HttpEntity<>(payload, headers), String.class);
ep6jt1vc

ep6jt1vc3#

尝试使用HttpClientBuilder
这样你就可以通过调用setEnableHttp(true)来设置enableHttp,如下所示:

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setConnectionTimeout(30);
httpClientBuilder.setSocketTimeout(30);
httpClientBuilder.setMaxConnTotal(20);
httpClientBuilder.setMaxConnPerRoute(2);
httpClientBuilder.setEnableHttp(true); // <- this will fix it

RestTemplate restTemplate = new RestTemplate(
    httpClientBuilder.newHttpComponentsClientHttpRequestFactory()
);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);

String response = restTemplate.getForEntity(
    "http://ip:port/h2h.php?channelid=acrs&posid=1",
    String.class,
    new HttpEntity<>(headers))
.getBody();

相关问题