简体   繁体   English

使用RestTemplate时出现消息“不支持HTTP协议”的异常

[英]Getting Exception with message “http protocol is not supported” when using RestTemplate

I'm trying to hit a URL using GET request using RestTemplate. 我正在尝试使用RestTemplate使用GET请求命中URL。 It is giving an exception stating http protocol is not supported. 它给出了一个异常,指出不支持http协议。

I'm able to get the expected response using plain old HttpURLConnection method to hit the endpoint. 我能够使用普通的旧HttpURLConnection方法获得预期的响应以命中端点。 But I'm unable to do so using the rest template. 但是我无法使用其余模板来做到这一点。 I'm not using any kind of VPN or proxy while trying this out. 尝试此操作时,我没有使用任何类型的VPN或代理。

Following is the code I'm using. 以下是我正在使用的代码。 I'll be replacing the actual ip and port used with ip:port. 我将用ip:port替换实际的ip和端口。

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);

Exception Trace 异常跟踪

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]

Any help will be appreciated. 任何帮助将不胜感激。

Thank you. 谢谢。

EDIT: Code with HttpUrlConnection 编辑:带有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) {

    }
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);

This is how I use RestTemplate : 这就是我使用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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Spring RestTemplate 请求 XSRF-TOKEN 时出现异常 - Getting exception when requesting XSRF-TOKEN using Spring RestTemplate 获取此异常 org.apache.http.conn.UnsupportedSchemeException: https 协议不受支持 - Getting this exception org.apache.http.conn.UnsupportedSchemeException: https protocol is not supported 尝试与resttemplate一起使用put时无法读取HTTP消息 - Getting failed to read HTTP message, while trying to use put with resttemplate 使用Spring时获取HttpMessageNotReadableException异常-RestTemplate - Get HttpMessageNotReadableException Exception when using Spring - RestTemplate 使用spring RESTtemplate检索Jsonobjects列表时出现异常 - i'm getting an exception when retrieving a list of Jsonobjects using springs RESTtemplate 记录由 RestTemplate 生成的异常时如何打印完整的错误消息? - How to print complete error message when logging an exception generated by RestTemplate? 协议异常不支持的地址族 - Address family not supported by protocol exception REST POST 与 POSTMAN 一起正常工作,但在使用 Spring RestTemplate 时出现异常 - REST POST works correctly with POSTMAN but exception when using Spring RestTemplate 使用 spring restTemplate 发送 post 请求时出现异常 - have an exception when using spring restTemplate to send a post request 当协议与 http 不同时,未使用 ApachePOI 在 Numbers 中添加超链接地址 - HyperLink address not getting added in Numbers using ApachePOI when protocol is different from http
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM