3
respostas

Teste de conexão com o servidor

Boa tarde, a todos!

Por favor alguém pode me dá uma dica para que eu consiga realizar meu teste de conexão com o servidor com sucesso? Pois, tenho erro de time out o tempo todo?

Segue abaixo código do teste e descrição do erro;

Código

package br.com.alura.loja;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.junit.Assert;
import org.junit.Test;

public class ClienteTest {

    @Test
    public void testeQueAConexaoComOServidorFunciona(){

        Client client = ClientBuilder.newClient();        
        WebTarget target = client.target("http://www.mocky.io");        
        String conteudo    = target.path("/v2/52aaf5deee7ba8c70329fb7d").request().get(String.class);    
        Assert.assertTrue(conteudo.contains("<rua>Rua Vergueiro 3185"));

    }

}

Mensagem de Erro

javax.ws.rs.ProcessingException: java.net.ConnectException: Connection timed out: connect
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:184)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:227)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:422)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
    at br.com.alura.loja.ClienteTest.testaQueAConexaoComOServidorFunciona(ClienteTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:276)
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:182)
    ... 33 more
3 respostas

Opa, tentei acessar aqui e parece que realmente esse endereço ta fora... Mas olha, vc pode testar para qualquer endereco e verificar se existe determinada string na volta.

Caro Alberto!

Encarecidamente você poderia me informar alguma URI de servidor válida para que eu pudesse continuar o curso?

Tentei por exemplo: o google maps, mas não tive sucesso, a forma como enderecei o target e o path no código abaixo está correta?

http://maps.googleapis.com/maps/api/geocode/xml?address=avenida+imperatriz+leopoldina+sao+paulo

public class ClienteTest {

    @Test
    public void testaQueAConexaoComOServidorFunciona(){

        Client client = ClientBuilder.newClient();        
        WebTarget target = client.target("http://maps.googleapis.com");        
        String conteudo    = target.path("/maps/api/geocode/xml?address=rua+santa+cecilia+sao+paulo").request().get(String.class);    
        Assert.assertTrue(conteudo.contains("<short_name>Av. Imperatriz Leopoldina"));
    }
}

Estou com o mesmo problema.. o pior é que não dá para fazer o resto dos exercícios pq precisamos do carrinho para os outros exercícios também