locked
SocketException: No connection could be made because the target machine actively refused (ipaddress) RRS feed

  • Question

  • User-1833521293 posted

    When I execute the following code to get the token, I got the error "SocketException: No connection could be made because the target machine actively refused (ipaddress)". What caused the error? Did I miss something? Thanks.

    using (var client = new HttpClient())
    {
       client.BaseAddress = new Uri("http://localhost:1234");
       client.DefaultRequestHeaders.Accept.Clear();
       client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                  
       var body = new List<KeyValuePair<string, string>>
       {
         new KeyValuePair<string, string>("grant_type", "password"),
         new KeyValuePair<string, string>("client_id", "client_id"),
         new KeyValuePair<string, string>("client_screte", "client_screte"),
         new KeyValuePair<string, string>("username", "xyz"),
         new KeyValuePair<string, string>("password", "Password"),
         new KeyValuePair<string, string>("scope", "scope"),
        };
    
        var content = new FormUrlEncodedContent(body);
        HttpResponseMessage tokenResponse = await client.PostAsync("/connect/token", content);
    }
    Tuesday, December 26, 2017 10:41 PM

Answers

  • User1120430333 posted

    What caused the error? Did I miss something? Thanks.

    Maybe a firewall is blocking the port, some other program is listening on the port or the port is restricted..

    https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 26, 2017 11:05 PM
  • User61956409 posted

    Hi danyeung,

    No connection could be made because the target machine actively refused

    According to your code, I can find that you make a request to API endpoint (that seems hosting on your local computer). Firstly, please check if your API app is running on your IIS or IIS express. Secondly, as DA924 said, you can check if that port is blocked. You can try to change the port for your API app and test if it can work with new port.

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 27, 2017 5:24 AM
  • User-782232518 posted

    When I execute the following code to get the token, I got the error "SocketException: No connection could be made because the target machine actively refused (ipaddress)". What caused the error? Did I miss something? Thanks.

    Such exceptions happen when nothing is there at port 1234 to respond to your HTTP requests. You can run "netstat -aon" at command prompt to see which process is listening at port 1234 (or nothing is there) to confirm.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 21, 2018 8:05 PM

All replies

  • User1120430333 posted

    What caused the error? Did I miss something? Thanks.

    Maybe a firewall is blocking the port, some other program is listening on the port or the port is restricted..

    https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 26, 2017 11:05 PM
  • User61956409 posted

    Hi danyeung,

    No connection could be made because the target machine actively refused

    According to your code, I can find that you make a request to API endpoint (that seems hosting on your local computer). Firstly, please check if your API app is running on your IIS or IIS express. Secondly, as DA924 said, you can check if that port is blocked. You can try to change the port for your API app and test if it can work with new port.

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 27, 2017 5:24 AM
  • User-1833521293 posted

    I ran it from Visual Studio with debug mode. Thanks. Does the code look correct?

    Wednesday, December 27, 2017 4:06 PM
  • User-474980206 posted

    you need to run the web application that is listening on port 1234, not just the client code.

    Wednesday, December 27, 2017 7:57 PM
  • User-1833521293 posted

    Thanks for all of you. The error was gone. Maybe someone else did something about it.

    Wednesday, December 27, 2017 11:56 PM
  • User1120430333 posted

    Thanks for all of you. The error was gone. Maybe someone else did something about it.

    No I don't think so.

    Friday, December 29, 2017 9:26 PM
  • User-782232518 posted

    When I execute the following code to get the token, I got the error "SocketException: No connection could be made because the target machine actively refused (ipaddress)". What caused the error? Did I miss something? Thanks.

    Such exceptions happen when nothing is there at port 1234 to respond to your HTTP requests. You can run "netstat -aon" at command prompt to see which process is listening at port 1234 (or nothing is there) to confirm.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 21, 2018 8:05 PM