Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Error in HTTPS connection with error: No connection could be made because the target machine actively refused it. at C:/Perl/lib/LWP/Protocol/http.pm line 47

by jliu5 (Initiate)
on Jul 26, 2016 at 15:36 UTC ( [id://1168570]=perlquestion: print w/replies, xml ) Need Help??

jliu5 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am trying to create a REST client to do a simple GET service at a https server, but got this error.

Here is my sample code, please advice.:

#! /usr/bin/env perl use REST::Client; use MIME::Base64; my $username = 'me'; my $password = 'me!!'; my $headers = {Accept => 'application/xml', Authorization => 'Basic ' +. encode_base64($username . ':' . $password)}; my $client = REST::Client->new(); $client->getUseragent->ssl_opts(verify_hostname => 0); $client->getUseragent->ssl_opts(SSL_verify_mode => SSL_VERIFY_NONE); $client->getUseragent->no_proxy('server_ip_string', 'server_ip_string' +); $client->GET("https://server_ip_string:port/upm-server/nbi/organizatio +n", $headers ); my $response = $client->responseContent(); print $response;
  • Comment on Error in HTTPS connection with error: No connection could be made because the target machine actively refused it. at C:/Perl/lib/LWP/Protocol/http.pm line 47
  • Download Code

Replies are listed 'Best First'.
Re: Error in HTTPS connection with error: No connection could be made because the target machine actively refused it. at C:/Perl/lib/LWP/Protocol/http.pm line 47 (ECONNREFUSED)
by tye (Sage) on Jul 26, 2016 at 16:16 UTC

    "Connection refused" means that the host at the specified IP address was successfully reached but that no service was registered to listen on the port number that you specified.1

    So there are really only a very few reasons that you could get that error:

    • You gave the wrong IP address
    • You gave the wrong port number
    • The service was not started
    • The service is only listening on some of the host's own IP addresses (most hosts only have 2 IP addresses, 127.0.0.1 and their other one, and a service can decide to only listen for connections sent to 127.0.0.1 or only to some other specific IP address -- this is actually a fairly common programming mistake when writing TCP service code).

    Looking at "netstat -a" on the service's host (if it is running Unix or Windows, at least) would tell you what ports are being listened to for which addresses. If you are on Unix, then you can use "sudo netstat -ap" to even see which process is listening on each port (on Windows you would use "netstat -ab" in a cmd prompt with elevated privileges).

    See also connect. The entry for ECONNREFUSED is quite short there but is accurate and would have been a very good clue.

    1 Or, quite rarely, it can be that a TCP service is listening on that port number and is not using the 'socket' library (which is, by far, the most commonly used library) to accept requests at that port number. In such a case, it is possible for the service to examine information in the first part of the TCP handshake (usually the source IP address, that is, the client's IP address) and reject the request in the same way that the TCP stack will reject a request for an unlistened to port number.

    - tye        

Re: Error in HTTPS connection with error: No connection could be made because the target machine actively refused it. at C:/Perl/lib/LWP/Protocol/http.pm line 47
by stevieb (Canon) on Jul 26, 2016 at 16:08 UTC

    Welcome to the Monastery, jliu5!

    First thing to do in this case is to verify that you can connect to the site by traditional means. Try via a web browser (https://server_ip), and/or telnet server_ip 443.

    Do either one of those connect properly? If so, are you sure you have the correct server string in your script?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1168570]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-24 00:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found