Java HTTP Request on VPN only website - java

I am trying to build a website parser for one of our internal websites (accessible only from the company network - we get on the network through Cisco AnyConnect VPN).
I can access the site fine in any browser, but not using HTTP requests. Windows network and sharing center shows that I have two active networks:
The actual internet connection
The company network (without internet access).
Default HTTP client gets time out as I suppose it makes a request using the actual internet connection (and the website is not accessible to public), but using this code:
HttpParams params = httpClient.getParams();
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName("10.x.x.x"));
I get the following error:
I/O exception caught when connectiong to /10.x.x.x -> {s} -> https://zzz.com:443: Network is unreachable: connect
Also, might be a stupid test but I have done a HTTP request to a "what is my ip" site and the IP is shown as my Wifi IP not the IP through VPN (which I get when I open browser and browse to a "what is my ip" website). Same thing (wrong IP) when I try this using a gui-less browser (Jaunt or HTMLUnit).
Please advise if any fixes for this.

ConnRoutePNames appears to be deprecated. See if the following works (I haven't tested):
HttpHost proxy = new HttpHost("10.x.x.x", 80);
HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpclient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();

Related

Cannot make http GET request on localhost of the android device/emulator

I have made test application to investigate the issue related to http request. It is an android application where I'm making request to localhost:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://127.0.0.1")
.build();
try {
Response response = client.newCall(request).execute();
Log.v("krv", response.toString());
} catch (Exception e) {
Log.v("krv", e.toString());
}
As result on device emulator I have "java.net.ConnectException: Failed to connect to /127.0.0.1:80" exception. With any other request on an external resource(like google.com) all are Ok. Also I have installed IIS. Operating system is Windows 10.
P.S. Really what I need - to be able to make http request on stb device where http server is run(where I have the same problem). I guess both issues have similar reason. Could you give me any advice how to fix/debug my problem...
127.0.0.1 is the loopback address for localhost. But when you're inside an android OS, the localhost or 127.0.0.1 is android itself and that is not where your server is running.
If you want to connect to the host machine, you need to find out the IP address of your host machine (Windows 10) and use that IP address instead of 127.0.0.1.
See How to find your local IP address
However, If you want to refer to the computer which is running the Android emulator, use the IP address 10.0.2.2 instead.

java - send https request to server ip address throws SSLHandshakeException

I need to send post request to server ip through HTTPS protocol. Here is my code:
HttpClient httpClient = HttpClients.createDefault();
String address = "https://100.100.100.100:90"; //just an example
HttpPost httpPost = new HttpPost(address);
httpPost.addHeader("charset", "UTF-8");
//set post data(not important here)
...
HttpResponse response = httpClient.execute(httpPost);
the last line throws this exception:
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
What's the problem and how can I solve this?
EDIT:
I don't know DNS name, so I need to use ip instead. I sent ajax request to that ip in browser, browser sends request, but requests stay in waiting mode. My Http Client is a simple java desktop application.
Probably the server you're trying to connect to doesn't accept requests using IP address.
In HTTP, this happens if the server is used for multiple domains / websites - it needs to know which one to give you and it does so based on the Host header. So if you want to connect by IP your client must be configured to send the correct Host (and must be able to do so).
Now in HTTPS protocol, on top of this the server also provides a server certificate during the handshake and this certificate is issued for a server hostname }likely a DNS name e.g. example.com) - again if you connect by IP and don't give your HTTP client any more info, the client cannot validate that the certifiate received from the server is issued for the correct hostname.

How to use DefaultHttpClient on Android?

I have made an application and used DefaultHttpClient() to access my local server.
The address looks like this: "http://192.168.1.104:8080/login".
I have successfully used it :)
But problem comes when I am in my university and have connected to the network and have an IP address like: "http://130.230.146.148:8080/login", which I am unable to connect and gets the following error:
java.net.ConnectException: failed to connect to /130.230.146.148 (port 8080): connect failed: EHOSTUNREACH (No route to host).
Although on my pc I can access "130.230.146.148:8080/".
How can I solve this problem?
The EHOSTUNREACH (No route to host) error means that a TCP attempt to connect was made and failed because the underlying protocol software did not find a network node to connect to the targeting node.
When you say that when you are in the university and trying to connect it means that you are probably within a private sub-net. So what you could possibly do is check if your Android device is also connected to that sub-net.
Maybe at university you are under a proxy server, try to find out what are proxy server IP and port (if you can access to the URL through your pc-browser, you have to have the browser configured for the proxy). Once you have it, try this
// Proxy server
HttpHost proxy = new HttpHost("your host", your proxy);
// httpClient is your DefaultHttpClient once instantiated
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
// If you need credentials, ignore otherwise
httpClient.getCredentialsProvider().setCredentials(
new AuthScope("your host", your proxy),
new UsernamePasswordCredentials(
"your username", "your password"));
Hope helps!
P.D: As others user says HttpClient is deprecated, you may consider move to HttpUrlConnection

Accessing Servlet from android httpclient programs getting connection refused?

While I access the localhost servlet from android httpclient utils I am getting connection refused error. The servlet is running fine, but I am getting error.
This is the code:
String dmurl="http://127.0.0.1:9090/DataManagerProject/DMServlet";
HttpClient httpClient = new DefaultHttpClient();
HttpGet hrtreq = new HttpGet(dmurl);
HttpResponse resp = httpClient.execute(hrtreq);
String output = EntityUtils.toString(resp.getEntity());
I am getting following error org.apache.http.conn.httphostconnectexception connection to refused
if you are using emulator to run your app for local server. mention the local ip as 10.0.2.2 and have to give Internet permission into your app
For more details, please refer this link.
Also, Turn Off the firewall and any anti-virus application in your PC
Check whether you have internet access enabled in your emulator and in your application
<uses-permission android:name="android.permission.INTERNET"/>
To access internet data of the device or emulator from your application add the above line in your manifest xml file.Also check the servlet mapping is proper in your the servlet project.
Assuming your servlet runs on your computer the reason you can't get a connection is because your using the wrong IP. 127.0.0.1 is a loopback address meaning that if you connect to 127.0.0.1 on your Android device, it will try to connect to the Android device.
To make it work you have to find the IP address of the computer which runs the servlet and use that in the URL instead.
In addition make sure you have given the Internet permission to your app.

java - can't make http request on port other than 80

My java program is hitting "http://url:port" kind of url to fetch some data. On my local windows machine deployed on tomcat 6, it is working fine. But on production which is a linux machine having tomcat 6 on it, it gives me connection timeout.
Ironically, if I hit the URL without port number, it will successfully bring me the output but not with port. Not finding any clue, please help.
The snippet of code I am using to connect and fetch data is:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("59.162.167.36:80/api/…");
httpget.setHeader("User-Agent", "UserAgent: Mozilla/5.0");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
One obvious possibility is that a firewall in front of your production machine is blocking access to that port. Check the firewall.
The answer is straightforward: On production you don't have that port opened, contact the administrator, or the hosting and issue your problem. Of course they will confirm my thesis.
Almost certainly your hosting provider implements a firewall of some description in the data center. This is common practice. Send them a message asking if port X is blocked, and if so can they open it.

Categories

Resources