I have the following code
public static void main(String[] args) {
Connection connection = Jsoup.connect("http://prospective.bryantschools.org/");
connection.timeout(5000);
Document doc;
try {
doc = Jsoup.connect("http://prospective.bryantschools.org/").get();
// get page title
String title = doc.title();
System.out.println("title : " + title);
// get all links
Elements links = doc.select("a[href]");
for (Element link : links) {
// get the value from href attribute
System.out.println("\nlink : " + link.attr("href"));
System.out.println("text : " + link.text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
And I am getting the following errors
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1168)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:932)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:512)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:194)
at com.example.kaleb.testjsoup.HTMLParserExample1.main(HTMLParserExample1.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
I've looked around and many people have mentioned that it might because the server's firewall is blocking my request. Is there is any other reason why I'd be getting these exceptions? Is there something I'm doing in my code that is casuing these exceptions?
Try Jsoup.connect(url).userAgent("Mozilla").get();. Our web server blocked it because it didn't have a user agent and blocks non-real-person-looking requests.
Related
I have 2 micro-services communicating via simple ResT call. I have a scenario where email needs to be send to user and admin , so there are two calls going from service 1 to service 2 .
**log.info("sending mail to user {}", ticketDetailsDto.getEmail());
// Send Email to User
**emailServiceUtils.emailServiceCall(notificationDtoForUser, ServiceEndPoint.EMAIL_URL_UNAUTHENTICATE);**
NotificationDto notificationDtoAdmin = new NotificationDto();
notificationDtoAdmin.setRecipient(applicationConfig.getSupportEmail());
notificationDtoAdmin.setTemplateId(EmailTemplate.SUPPORT_ADMIN);
notificationDtoAdmin.setData(dataOfUser);
log.info("sending mail to {}", applicationConfig.getSupportEmail());
log.debug("email service url {}", applicationConfig.emailServiceInstance() + ServiceEndPoint.EMAIL_URL_UNAUTHENTICATE);
//Send Email to Admin
emailServiceUtils.emailServiceCall(notificationDtoAdmin, ServiceEndPoint.EMAIL_URL_UNAUTHENTICATE);
return "Your request sent to support team";**
email service call method is like this .
public String emailServiceCall(NotificationDto notificationDto, String url) {
try {
UriComponentsBuilder uriBuilder = getUriComponentsBuilder(url);
log.info("email service url {}", uriBuilder.build().toUri());
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
requestHeaders.add(Constants.AUTHORIZATION, requestScopedStorageService.getToken());
restTemplate.setErrorHandler(new ExceptionHandler());
HttpEntity<?> requestEntity = new HttpEntity<>(notificationDto, requestHeaders);
restTemplate.exchange(uriBuilder.build().toUri(), HttpMethod.POST, requestEntity, String.class);
} catch (RestClientException e) {
e.printStackTrace();
}
return "email send";
}
Now calls have started failing.
2021-08-18 10:25:14.262 ERROR 1 --- [nio-8089-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://email-service/email/sendThroughTemplateUnAuthenticated": Connection timed out (Connection timed out); nested exception is java.net.ConnectException: Connection timed out (Connection timed out)] with root cause
java.net.ConnectException: Connection timed out (Connection timed out)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_302]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_302]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_302]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_302]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_302]
at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_302]
at java.net.Socket.connect(Socket.java:556) ~[na:1.8.0_302]
at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.8.0_302]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[na:1.8.0_302]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[na:1.8.0_302]
at sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[na:1.8.0_302]
at sun.net.www.http.HttpClient.New(HttpClient.java:339) ~[na:1.8.0_302]
at sun.net.www.http.HttpClient.New(HttpClient.java:357) ~[na:1.8.0_302]
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1226) ~[na:1.8.0_302]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1162) ~[na:1.8.0_302]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1056) ~[na:1.8.0_302]
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:990) ~[na:1.8.0_302]
This started happening after I upgraded spring boot from 2.2.5.RELEASE to 2.4.6 , Interesting thing is this works if I downgrade to 2.2.5.RELEASE.
This question already has answers here:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
(51 answers)
Closed 4 years ago.
I am creating an application and I need to connect to a remote MySql DB. I am using JSch to connect via SSH. I am able to connect via JSch, however when I try to connect to my database I keep getting errors.
public void connectSSH() {
int lport = 5656;
String rhost = "host"; //not actual host
String host = "host"; //not actual host
int rport = 3306;
String user = "user"; //not actual user
String password = "*********";
String dbuserName = "user"; //not actual user
String dbpassword = "********";
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:" + rport + "?user=" + dbuserName + "&password=" + dbpassword + "&useSSL=true";
Connection conn = null;
Session session = null;
try {
//Set StrictHostKeyChecking property to no to avoid UnknownHostKey issue
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
int assinged_port = session.setPortForwardingL(lport, rhost, rport);
System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport);
System.out.println("Port Forwarded");
//mysql database connectivity
Class.forName(driverName).newInstance();
conn = DriverManager.getConnection(url);
System.out.println("Database connection established");
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null && !conn.isClosed()) {
System.out.println("Closing Database Connection");
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
if (session != null && session.isConnected()) {
System.out.println("Closing SSH Connection");
session.disconnect();
}
}
}
Output:
Connected
localhost:5656 -> rhost:3306
Port Forwarded
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago.The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at com.ej.ozyazgan.classes.SqlHelper.connectSSH(SqlHelper.java:176)
at com.ej.ozyazgan.classes.MainController.initialize(MainController.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at com.ej.ozyazgan.classes.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:301)
... 43 more
Closing SSH Connection
The url cannot be null
Caused by: java.net.ConnectException: Connection refused: connect
You didn't read the stack trace. The actual error here is 'connection refused'. The port in the JDBC URL should be the local SSH forwarding port, not the remote port.
The code works when the server and client are on the same machine. When I run the server code on a different machine, I get the exception below.
[WARNING]
java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is:
java.net.ConnectException: Operation timed out (Connection timed out)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:129)
at javax.management.remote.rmi.RMIServerImpl_Stub.newClient(Unknown Source)
at javax.management.remote.rmi.RMIConnector.getConnection(RMIConnector.java:2430)
at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:308)
at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:270)
at Client.main(Client.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Operation timed out (Connection timed out)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 14 more
It's suspicious that the exception says 127.0.1.1 refused the connection when the client really used the JMXServiceURL below:
service:jmx:rmi:///jndi/rmi://10.10.173.196:8474/jmxrmi
If I start the application using the -Dcom.sun.management.jmxremote.* settings, the client doesn't not have an issue with remote connections.
Code is pasted below:
import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.util.*;
public class Server {
public static void main(String[] args) throws Exception {
if(args.length != 2) {
throw new IllegalArgumentException("Server <port> <path suffix>"
+ "\nservice:jmx:rmi://0.0.0.0:<port>/jndi/rmi://0.0.0.0:<port>/<pathsuffix>"
);
}
int port = Integer.parseInt(args[0]);
String pathSuffix = args[1];
LocateRegistry.createRegistry(port);
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://0.0.0.0:"
+ port + "/jndi/rmi://0.0.0.0:" + port + "/" + pathSuffix);
Map<String, Object> envConf = new HashMap<>();
envConf.put("jmx.remote.x.password.file", "password.properties");
envConf.put("jmx.remote.x.access.file", "access.properties");
JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, envConf, mbs);
cs.start();
while(true) {
Thread.sleep(1000);
}
}
}
I also used the JMXServiceURL on the server:
service:jmx:rmi:///jndi/rmi://0.0.0.0:<port>/jmxrmi
https://stackoverflow.com/a/11654322/1810962 mentions that /etc/hosts must be configured properly. On the machine that JMX works I have:
hostname -i
10.10.172.72 10.10.172.72
On the machine that doesn't work:
hostname -i
127.0.1.1
I'm working on a project on Codename One in which I need to check certain addresses. If the address doesn't exist, a variable in my code, named lectura should be 404. However when I find an address which doesn't exist, my code stops and displays a message on screen thats says java.net.SocketTimeoutException with the options cancel or retry. I need my program to know the address didn't exist and to move on, not to pause. My code is:
public int readCNO(String cantidad, int number){
ConnectionRequest r3 = new ConnectionRequest();
r3.setUrl("http://" + ipZona + "/arduino/" + cantidad + "!" + type + "/" + Integer.toString(number));
r3.setPost(false);
r3.setDuplicateSupported(true);
r3.setTimeout(100);
NetworkManager.getInstance().addToQueueAndWait(r3);
r3.addResponseListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
try
{
NetworkEvent event = (NetworkEvent) ev;
byte[] data= (byte[]) event.getMetaData();
String decodedData = new String(data,"UTF-8");
System.out.println(decodedData);
lectura = Integer.parseInt(decodedData.trim());
} catch (Exception e)
{
//ex.printStackTrace();
lectura = 404;
}
}
});
NetworkManager.getInstance().addToQueue(r3);
//NetworkManager.getInstance().killAndWait(r3);
return lectura;
}
Help really appreciated!
David.
The complete exception is shown afterwards, but It doesn't refer to any of my classes, not where the code above is, nor where it is called.
java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1300)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at com.codename1.impl.javase.JavaSEPort.getResponseCode(JavaSEPort.java:4557)
at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:330)
at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:261)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:996)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:850)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1300)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
at com.codename1.impl.javase.JavaSEPort.getResponseCode(JavaSEPort.java:4557)
at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:330)
at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:261)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
I solved my problem with an answer from this post catching unknown host exception in codename one, the code is:
NetworkManager.getInstance().addErrorListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//handle your error here consume the event
evt.consume();
}
});
For some reason I receiving java.net.ConnectException: Connection timed out from the following code below. It works fine on my local computer, and I can access it via the browser both on my local and dev server, however on the dev server I get this error when trying to read it.
public List<NodeMap> downloadFile(String fileUrl) {
InputStream inputStream = null;
try {
URL url = new URL(fileUrl);
URLConnection con = url.openConnection();
con.setConnectTimeout(60000);
con.setReadTimeout(60000);
inputStream = con.getInputStream();
CSVReader csvReader = new CSVReader(new InputStreamReader(inputStream),',','"');
return iterateRows(csvReader);
}
catch (IOException e) {
LOG.error("Node mapping file", e);
}
finally {
IOUtils.closeQuietly(inputStream);
}
return null;
}
Full Stack
java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.7.0_21]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[na:1.7.0_21]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[na:1.7.0_21]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[na:1.7.0_21]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) ~[na:1.7.0_21]
at java.net.Socket.connect(Socket.java:579) ~[na:1.7.0_21]
at sun.net.NetworkClient.doConnect(NetworkClient.java:175) ~[na:1.7.0_21]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:378) ~[na:1.7.0_21]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:473) ~[na:1.7.0_21]
at sun.net.www.http.HttpClient.<init>(HttpClient.java:203) ~[na:1.7.0_21]
at sun.net.www.http.HttpClient.New(HttpClient.java:290) ~[na:1.7.0_21]
at sun.net.www.http.HttpClient.New(HttpClient.java:306) ~[na:1.7.0_21]
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:995) ~[na:1.7.0_21]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:931) ~[na:1.7.0_21]
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:849) ~[na:1.7.0_21]
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1299) ~[na:1.7.0_21]
Looks like it was a DNS issue. I used the internal IP of the url, and it worked!