0

I am getting connection timeout: connect exception when I used username and password in url. url is "http://testadmin:testadmin@myhostName/manager/text/list";

This url working in chrome, firefox web browser but when i am going to access this url through java code.

Here is the output and exception I get:

&&&&&&&&&&&&&&&
*********88
response
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 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.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at test.TomcatTest.main(TomcatTest.java:23)

I'm using the following code.

package test;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class TomcatTest {


    public static void main(String[] args) {
        String listUrl = "http://testadmin:testadmin@myhostName/manager/text/list";
        String serverResponse = "";
        URL url = null;
        try {
            System.out.println("&&&&&&&&&&&&&&&");
            url = new URL(listUrl);
            URLConnection connection = url.openConnection();
            System.out.println("*********88");
            // i also tried without setting readtimeout.
            connection.setReadTimeout(3 * 60 * 1000);// set timeout 3 minutes
            InputStream inputStream = connection.getInputStream();
            System.out.println("^^^^^^^^^^^^^");
            int chr = -1;
            while ((chr = inputStream.read()) != -1) {
                System.out.print((char)chr);
                serverResponse += (char)chr;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("response " + serverResponse);
    }
}
jopasserat
  • 5,304
  • 4
  • 29
  • 48
Ajay Yadav
  • 49
  • 8

2 Answers2

1

The URL http://<user>:<pass>@url is not send in this way to the web server. The Browser takes username and password away from the url and creates a basic authentication header.

Java literally sends the URL to the server. Which is a security problem, because the URL might be logged at several stages.

This demo code is 99% based on answer by Wanderson Santos and joel234 with Java 8 Base64 adaptions:

String _url_string = "http://server/"

url = new URL(_url_string);
URLConnection uc = url.openConnection();
String userpass = _user + ":" + _password;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
InputStream in = uc.getInputStream();
Community
  • 1
  • 1
notes-jj
  • 1,072
  • 13
  • 28
  • Thanks @notes-ji, This is relevant to me. – Ajay Yadav Dec 17 '15 at 16:09
  • @AjayYadav If you feel my answer was useful, than vote for it. If you feel my answer is solving your questing than click the hook. Change the title of the problem to somethong more meningful "Getting connection timeout: URL.openConnection()" Thanks – notes-jj Dec 17 '15 at 17:26
0

Now I've got a solution that we can not used username and password in url like as above.

It can be used like this:

String listUrl = "http://myHostName/manager/text/list";
//use username and password here
connection.setRequestProperty("Authorization", String.format("Basic %s", new BASE64Encoder().encode("testadmin:testadmin".getBytes("UTF-8"))));
jopasserat
  • 5,304
  • 4
  • 29
  • 48
Ajay Yadav
  • 49
  • 8