HttpClient ERROR: Connection refused: no further information: localhost/127.0.0.1:80

2,139 views
Skip to first unread message

Manjunath Reddy

unread,
Dec 15, 2016, 2:23:56 AM12/15/16
to vert.x
Hi,

I have written simple code to download the zip file from URL but the vertx (Netty) is throwing connection refused error but not throwing further information for debug. Below is my code.

package com.tsc.j2v8_tsc;


import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;


/**
 *
 * @author Kushan
 */

public class AssetCompiler {


   
static String _URL = "https://registry.npmjs.org/npm/-/npm-4.0.3.tgz";


   
public static void main(String[] args) throws Exception {


       
File file = new File("C:\\Manju\\games\\filess", "npm-4.0.3.tgz");
       
if (!file.exists()) {
            file
.createNewFile();
       
}


       
Vertx vtx = Vertx.vertx();
       
HttpClient client = vtx.createHttpClient();
       
try {
            client
.getNow(_URL, res -> {
                res
.bodyHandler(body -> {
                   
System.out.println("Writing content...");
                    vtx
.fileSystem().writeFileBlocking(file.getPath(), body);
               
});


                res
.endHandler(end -> {
                   
System.out.println("Writing content ends...");
                    unzip
(file);
                    client
.close();
               
});
                res
.exceptionHandler(err -> {
                    err
.printStackTrace();
                    client
.close();
               
});
           
});
       
} catch (Exception e) {
            e
.printStackTrace();
       
}
   
}


   
private static void unzip(File file) {
       
InputStream is;
       
try {
           
is = new FileInputStream(file);
           
TarArchiveInputStream tar = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
           
TarArchiveEntry entry;


           
while ((entry = tar.getNextTarEntry()) != null) {
               
final File outputFile = new File(file.getParentFile(), entry.getName());
               
if (entry.isDirectory()) {
                   
System.out.println(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                   
if (!outputFile.exists()) {
                       
System.out.println(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
                       
if (!outputFile.mkdirs()) {
                           
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                       
}
                   
}
               
} else {
                   
if (!outputFile.exists()) {
                        outputFile
.getParentFile().mkdirs();
                   
}
                   
System.out.println(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
                   
final OutputStream outputFileStream = new FileOutputStream(outputFile);
                   
IOUtils.copy(tar, outputFileStream);
               
}
           
}
            tar
.close();
           
is.close();
       
} catch (Exception ex) {
           
Logger.getLogger(AssetCompiler.class.getName()).log(Level.SEVERE, null, ex);
       
}


   
}
}

The same works if I use Apache http client or Simple URL connection. But Vertx client is failing to download. Any clue what is wrong here?

Thanks,
Manjunath Reddy

Buddha Shrestha

unread,
Dec 15, 2016, 2:37:47 AM12/15/16
to vert.x
Probably, because your trying to connect to https. 
Try to GET from a simple http request first which can verify that your code is fine.

In order to GET from a HTTPS source, you will need a certificate which will acknowledge the request.

Manjunath Reddy

unread,
Dec 15, 2016, 3:15:48 AM12/15/16
to vert.x
If certificate is the real issue, why would it get downloads when I use Apache HttpClient? Below code works (note no certificate needed here)



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tsc.j2v8_tsc;



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;



/**
 *
 * @author Kushan
 */

public class AssetCompiler {


   
static String _URL = "https://registry.npmjs.org/npm/-/npm-4.0.3.tgz";


   
public static void main(String[] args) throws Exception {


       
File file = new File("C:\\Manju\\games\\filess", "npm-4.0.3.tgz");
       
if (!file.exists()) {
            file
.createNewFile();
       
}



       
HttpClient client = HttpClients.createDefault();
       
HttpGet download = new HttpGet(_URL);
        client
.execute(download, (HttpResponse response) -> {
           
System.out.println(response.getEntity().getContentLength());
           
IOUtils.copy(response.getEntity().getContent(), new FileOutputStream(file));
            unzip
(file);
           
return null;

       
});
   
}


   
private static void unzip(File file) {

       
final byte[] signature = new byte[12];
       
try {
           
InputStream in = new FileInputStream(file);
           
in.mark(signature.length);
           
int l = in.read(signature);
           
in.reset();
           
CompressorInputStream cis;
           
if (BZip2CompressorInputStream.matches(signature, l)) {
                cis
= new BZip2CompressorInputStream(in);
           
} else if (GzipCompressorInputStream.matches(signature, l)) {
                cis
= new GzipCompressorInputStream(in);
           
} else {
                cis
= new LZMACompressorInputStream(in);
           
}


           
TarArchiveInputStream ais = new TarArchiveInputStream(cis);
           
TarArchiveEntry entry;
           
while ((entry = ais.getNextTarEntry()) != null) {

               
final File outputFile = new File(file.getParentFile(), entry.getName());
               
if (entry.isDirectory()) {
                   
System.out.println(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                   
if (!outputFile.exists()) {
                       
System.out.println(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
                       
if (!outputFile.mkdirs()) {
                           
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                       
}
                   
}
               
} else {
                   
if (!outputFile.exists()) {
                        outputFile
.getParentFile().mkdirs();
                   
}
                   
System.out.println(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
                   
final OutputStream outputFileStream = new FileOutputStream(outputFile);

                   
IOUtils.copy(ais, outputFileStream);
               
}
           
}
            ais
.close();
       
} catch (Exception e) {
            e
.printStackTrace();
       
}
   
}
}


Jez P

unread,
Dec 15, 2016, 3:46:20 AM12/15/16
to vert.x
You also need to tell the vert.x client which port to connect on. By default it will connect on 80 (http default). I don't think it will infer 443 from the https prefix. 

Jez P

unread,
Dec 15, 2016, 3:48:36 AM12/15/16
to vert.x
You're also using the wrong version of getNow: read the doc. The version you're using has the following JavaDoc (note specifically DEFAULT HOST AND PORT)

HttpClient getNow(String requestURI,
                  Handler<HttpClientResponse> responseHandler)
Sends an HTTP GET request to the server at the default host and port, specifying a response handler to receive the response
Parameters:
requestURI - the relative URI
responseHandler - the response handler
Returns:
a reference to this, so the API can be used fluently

On Thursday, December 15, 2016 at 8:15:48 AM UTC, Manjunath Reddy wrote:
Reply all
Reply to author
Forward
0 new messages