proxy trouble on eclipse with https - java

I'm having trouble sending https requests from eclipse.When I run this code it works fine.
public class HttpConnectionTest extends TestCase{
#Test
public void testConnection() throws HttpException, IOException {
int statusCode = new HttpClient().executeMethod(new GetMethod("http://www.google.com"));
assertTrue(statusCode == HttpStatus.SC_OK);
}
}
But when i run the same code for "https://www.google.com", I get the exception below;
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
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.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618)
at sun.security.ssl.SSLSocketImpl.<init>(SSLSocketImpl.java:451)
at sun.security.ssl.SSLSocketFactoryImpl.createSocket(SSLSocketFactoryImpl.java:140)
at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:81)
at org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory.createSocket(SSLProtocolSocketFactory.java:126)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:706)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:386)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
at adcWeb.HttpConnectionTest.testConnection(HttpConnectionTest.java:26)
I'm connecting to internet via proxy. My network setting are like this; I saw some people said to disable socks but eclipse doesn't allow me uncheck socks option (I'm using eclipse luna)

I believe that the proxy you configure in the eclipse settings are for plugins and IDE related stuff..
You will have to setup the proxy in your code at runtime like so :
How do I make HttpURLConnection use a proxy?

Related

Java : Io exception: The Network Adapter could not establish the connection

Currently i am trying to connect Oracle 11g database from tomcat server and i am getting following error :
Caused by: java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:154)
at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:145)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:205)
at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:169)
at org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:50)
... 34 more
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:392)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:434)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:687)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:247)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320)
... 45 more
Caused by: java.net.ConnectException: Connection timed out: connect
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 oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:150)
at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:370)
... 50 more
I thought it would be some kind of firewall issue or database down issue.
So i tried to connect through SQL Developer and it was working fine.
I tried to connect by running following java program :
public class RunDB {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:#9.2.2.2:1521:ORCL", "user", "pwd");
}
}
Still same error occurs :
Exception in thread "main" java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:147)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:257)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:389)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:454)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:802)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.mkyong.test.RunDB.main(RunDB.java:13)
I am using java 8 and Spring Tool Suite ide.
My JVM is using IPv6 protocol.
If not is there any way to fix this issue ?
Finally i fixed this issue , this error is caused by the JVM which is using the IPv6 protocol which is not supported by the network/oracle server.
Since i am using Tomcat server for my application i added following argument to tomcat
-Djava.net.preferIPv4Stack=true
or you can add this argument as System environment variable as :
Varable name : _JAVA_OPTIONS
Variable value: -Djava.net.preferIPv4Stack=true

JVM and SocksProxy Settings do not work on Ubuntu 16.04

Basic information:
I am trying to get a Java application to connect to a target via SOCKS Proxy. I found this on finding out how to configure proxy settings, which is important, as I cannot change the source code to configure Proxy usage. However, the application did not connect via the proxy.
Details
I am using a small custom class for testing this, which has exactly one main method and does this:
public static void main(String... strings) throws URISyntaxException, IOException {
URL url = new URI("http", null, "localhost", 9998, null, null, null).toURL();
System.out.println("opening connection to " + url.toString());
URLConnection c = url.openConnection();
System.out.println("connecting");
c.connect();
System.out.println(c.getContentLength());
System.out.println(c.getContentType());
}
Then, I start that class using java <properties> <classname>.
Properties I have tried so far (single and combined):
socksPropertyHost=localhost
socksPropertyPort=9999
http.proxySet=true
http.proxyHost=localhost
http.proxyPort=9999
https.proxySet=true
https.proxyHost=localhost
https.proxyPort=9999
proxySet=true
proxyHost=localhost
proxyPort=9999
java.net.useSystemProxies=true
Likewise, I added these on the command line, added then to my environment, set the values in jre/lib/net.properties and pout them into a comma-separated list which was then passed as -D$JAVA_OPTS in a bash script.
All of the connections were refused with
Exception in thread "main" java.net.ConnectException: Connection refused (Connection refused)
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 sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:463)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:558)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
at sun.net.www.http.HttpClient.New(HttpClient.java:339)
at sun.net.www.http.HttpClient.New(HttpClient.java:357)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1202)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1138)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1032)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:966)
at sandbox.Sandbox.main(Example.java:16)
Basically, the question(s) here is/are this/these:
Am I doing something wrong here? Is that a bug? Is it just my System?
Any answer would be greatly appreciated.
Please notice the line
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
in the stack trace. You have configured your JVM to try a SOCKS5 proxy, but the proxy is not responding where the JVM tries to connect. Typically SOCKS proxies live on port 1080.

DocumentDB Java SDK behind a proxy

I'm trying to create a simple web application from the todo tutorial.
As I'm behind a corporate proxy, I used the following common java system properties :
-Dhttp.proxyHost=myproxy Dhttp.proxyPort=3128
-Dhttps.proxyHost=myproxy -Dhttps.proxyPort=3128
I also try the following parameter:
-Djava.net.useSystemProxies=true
I still have a client exception
java.lang.IllegalStateException: Http client execution failed.
at com.microsoft.azure.documentdb.GatewayProxy.performPostRequest(GatewayProxy.java:350)
at com.microsoft.azure.documentdb.GatewayProxy.doSQLQuery(GatewayProxy.java:135)
at com.microsoft.azure.documentdb.DocumentClient.doQuery(DocumentClient.java:2013)
(...)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
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.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:625)
I would like to know if the azure documentdb sdk enable this proxy settings ? Maybe there is another way to set https proxy for this SDK ?
Following pull request merge, if you want to use a proxy you need to get a release version greater than 1.0.1.
As the release is not available at this moment, you could build github source and use the sdk snapshot version.
Then you just need to setup your proxy through configuration:
-Dhttp.proxyHost=myproxy Dhttp.proxyPort=3128
-Dhttps.proxyHost=myproxy -Dhttps.proxyPort=3128

Caused by: java.net.ConnectException: Connection refused Cloudify CLI

I have just downloaded cloudify2.6.2. When running the CLI of the cloudify i can not run the REST API as i get this error
Starting Non-Interactive Shell
>>> connect http://$localhost:8100
http://$localhost:8100/service/testrest Rest api error: Operation failed. org.apache.http.conn.HttpHostConnectException: Connection to http://$localhost:8100 refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:190)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at org.cloudifysource.restclient.GSRestClient.executeHttpMethod(GSRestClient.java:371)
at org.cloudifysource.restclient.GSRestClient.get(GSRestClient.java:278)
at org.cloudifysource.restclient.GSRestClient.get(GSRestClient.java:265)
at org.cloudifysource.shell.rest.RestAdminFacade.doConnect(RestAdminFacade.java:87)
at org.cloudifysource.shell.AbstractAdminFacade.connect(AbstractAdminFacade.java:55)
at org.cloudifysource.shell.commands.Connect.doExecute(Connect.java:62)
at org.cloudifysource.shell.commands.AbstractGSCommand.execute(AbstractGSCommand.java:99)
at org.apache.felix.gogo.commands.basic.AbstractCommand.execute(AbstractCommand.java:35)
at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:474)
at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:400)
at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:183)
at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120)
at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:89)
at org.apache.karaf.shell.console.jline.Console.run(Console.java:172)
at org.apache.karaf.shell.console.Main.run(Main.java:191)
at org.apache.karaf.shell.console.Main.run(Main.java:89)
at org.cloudifysource.shell.GigaShellMain.main(GigaShellMain.java:126)
Caused by: java.net.ConnectException: Connection refused
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 org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:127)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
... 24 more
Communication Error accessing http://$localhost:8100/service/testrest. Reason: Connection to http://$localhost:8100 refused
I do not have any firewall and i also use jdk7.
I need some explanation on this.
Have you bootstrapped Cloudify on your machine by using bootstrap-localcloud? Also, it seems you're using $localhost instead of just localhost.
There are some things to note here.
First for the REST service to work it has to be up, so a bootstrapping procedure has to be done, THEN do the connect action.
In my situation it was something wrong in the setenv.sh located at ~/gigaspaces/bin/setenv.sh.
There were two declarations that was not supposed to be there.
One for the NIC and one for the LOOKUPLOCATOR, so i comment out them and i finally did bootstrap the cloud.
Cloudify Version 2.6.0
Danos

Java RMI UnmarshalException and AWS EC2 Server

I'm trying to implement the RMI Compute PI tutorial found on the Sun/Oracle website here. The server is located on an Amazon windows EC2 Server. The client is located on my personal computer at home. Just to keep things simple, I have disabled the firewall on the server and open all the port on the EC2 security group. I can ping the machine, have access to files, etc... My aim is just to have a basic RMI program worked on AWS. I will oversee security issues later.
Lauching the server is not a problem. And it's correctly bound to the RMI Registry. I'm using the following command to launch it :
java -Djava.security.manager -Djava.security.policy=C:\AWSTest\security.policy -Djava.rmi.server.codebase=file:/C:/AWS/Sources/sun-rmi-tutorial-server/bin/ file:/C:/AWS/Sources/sun-rmi-tutorial-common/bin/ -classpath C:\AWS\Sources\sun-rmi-tutorial-server\bin;C:\AWS\Sources\sun-rmi-tutorial-common\bin engine.ComputeEngine ec2-XX-XX-XX-XXX.ap-southeast-1.compute.amazonaws.com
The RMI registry is started on the server side.
I have declared a new security manager on the server side System.setSecurityManager(new RMISecurityManager())
I'm using a security.policy file where I'm granted all permission.
When I'm launching the client side, on my local computer, I'm ending with an UnmarshalException:
ComputePi exception: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: engine.ComputeEngine_Stub
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: engine.ComputeEngine_Stub
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at client.ComputePi.main(ComputePi.java:14)
Caused by: java.lang.ClassNotFoundException: engine.ComputeEngine_Stub
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:453)
at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:184)
at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:637)
at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:264)
at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:216)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
... 3 more
Do you have any suggestions of what is wrong ? Why can't the client find the correct Stub ? I'm using Eclipse Juno, Genady RMI Plugin and Java 1.7.0_07.
Thanks for your help
EDIT 1 :
The Java workspace is organized around 3 projects : the Client side, the Server Side and a Common project where both the server and the client share classes.
I can read the remote registry from my local computer. The RMI plugin can connect remotely to the server and inspect the registry.
If instead of making 3 projects I put all the classes under the same project, everything is working fine... But I notice that no stubs where generated (or needed).
If, on the client side, I'm putting a reference in the classpath to the server classes (everything being build locally), i have a connection timeout error instead of the unmarshalling error.
ComputePi exception: Connection refused to host: XX.XXX.XX.XX; nested exception is:
java.net.ConnectException: Connection timed out: connect
java.rmi.ConnectException: Connection refused to host: XX.XXX.XXX; nested exception is:
java.net.ConnectException: Connection timed out: connect
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:128)
at engine.ComputeEngine_Stub.executeTask(ComputeEngine_Stub.java:35)
at client.ComputePi.main(ComputePi.java:19)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
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.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.(Socket.java:425)
at java.net.Socket.(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 5 more
You haven't deployed the stub class to the client.

Categories