Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HttpConnection refused, but curl equivalent works

I'm losing my mind on this one. My curl command works:

curl http://testuser:testpwd@qabox3:8501/xmlcontroller

But, when I try what looks like an equivalent http connection in Java it gives "connection refused". What am I missing? I've tried a dozen flavors of trying to make this connection today and am out of ideas.

        URL url = new URL( "http://qabox3:8051/xmlcontroller" );
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod( "GET" );
        String encoding = new sun.misc.BASE64Encoder().encode( "testuser:testpwd".getBytes() );
        conn.setRequestProperty("Authorization", "Basic " + encoding );
        InputStream content = conn.getInputStream();  // <--- fails here every time.
        BufferedReader in = new BufferedReader( new InputStreamReader( content ) );
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println( line );
        }

Moreover, I can use Java's Runtime.exec() to exec the curl command and that still works...so I'm clearly doing something wrong in the HttpURLConnection stuff.

Here's the stack I'm seeing (now using HttpClient, but basically same stack with the Java libs).

Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:520)
at java.net.Socket.connect(Socket.java:470)
at java.net.Socket.<init>(Socket.java:367)
at java.net.Socket.<init>(Socket.java:240)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at MyClass.sendRequest(iQ411RequestHandlerProxy.java:277)

Just for fun, here's the curl verbose output. Nothing special in the header...

> GET /xmlcontroller HTTP/1.1
> Authorization: Basic cWFfc3VwZXI6cWFfc3VwZXI=
> User-Agent: curl/7.16.3 (i686-pc-cygwin) libcurl/7.16.3 OpenSSL/0.9.8k zlib/1.2.3 libssh2/0.15-CVS
> Host: qabox3:8501
> Accept: */*
>
like image 767
Chris Kessel Avatar asked Sep 11 '25 02:09

Chris Kessel


1 Answers

You seem to use two different port numbers: 8501 and 8051? Could that be the problem or was that a typo in posting the question?

curl http://testuser:testpwd@qabox3:8501/xmlcontroller

URL url = new URL( "http://qabox3:8051/xmlcontroller" );

Regards, Ian

like image 115
IanGalpin Avatar answered Sep 13 '25 16:09

IanGalpin