Search code examples
c#http-request

Invalid URI: Invalid port specified in C# on specifying URL with port number in it like http://localhost:8080/jasperserver/rest


I created a simple function to execute an Http PUT request -

public string checkIfUserExists(string userName)
    {
        var endPoint = new Uri("http://localhost:8080/jasperserver/rest_v2/users/"+userName);

        var request = (HttpWebRequest)WebRequest.Create(endPoint);
        request.Method = "PUT";
        request.ContentType = "urlencoded";

        var response = (HttpWebResponse)request.GetResponse();

        return "Success";
    }

when I execute this, I get an exception "Invalid URI: Invalid port specified" at the line -

var endPoint = new Uri("http://localhost:8080/jasperserver/rest_v2/users/"+userName);

Any ideas for fixing this? Is the problem with localhost:8080 portion of the URL?


Solution

  • I fixed this error using string.Concat() method:-

    var endPoint = new Uri(string.Concat("http://localhost:8080/jasperserver/rest_v2/users/", userName));