Java HttpConnection refused, but works with curl

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

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

But when I try to use an equivalent http connection in Java, it gives a "connection failure". What am I missing? I tried a dozen tastes, trying to make this connection today, and I am not aware of the 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 );
        }

Alternatively, I can use Java Runtime.exec () to execute the curl command and which still works ... so I'm obviously doing something wrong in the HttpURLConnection materials.

Here is the stack I see (now using HttpClient, but basically the same stack with Java libraries).

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 is a long apical conclusion. Nothing special in the title ...

> 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: */*
>
+3
4
+10

HttpClient. , / .

0

HTTP-? HTTP- Java- ?

0

In your code example, you never call conn.connect () . Calling openConnection () creates a URLConnection object , but you need to actually call the connection method before interacting with the connection.

0
source

Source: https://habr.com/ru/post/1714554/


All Articles