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

4

7

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?

Jackass answered 31/3, 2015 at 11:43 Comment(0)
J
2

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

var endPoint = new Uri(string.Concat("http://localhost:8080/jasperserver/rest_v2/users/", userName));
Jackass answered 11/4, 2015 at 10:24 Comment(0)
M
7

I was facing this problem and found out that it is my carelessness causes this issue. I was getting the issue while I create an instance of HttpRequestMessage as follows.

var request = new HttpRequestMessage(new HttpMethod("PUT"), 
                    $"{_nextcloudConfigurationProvider.NextcloudBaseUrl}{FileConstants.ApiUploadFile.Replace("{UserName}", UserName)}");

enter image description here

The issue was I missed a slash "/" before my second constant. It was remote.php/dav/files/{UserName}/{directoryName}/ instead of /remote.php/dav/files/{UserName}/{directoryName}/. Adding the slash fixed the issue for me.

Just sharing this for the people who miss silly things, like me.

Macknair answered 5/3, 2019 at 13:31 Comment(0)
H
6

Have a look at this question Invalid URI: Invalid port specified when url has more than one colon

Have you checked your username for characters that could cause a problem?

EDIT after comment - The reason that I referenced this question was because the 'Invalid Port' error occurred not because the actual port was wrong, but because of other invalid characters in the URL. Validating that the username is correctly encoded will prevent this problem.

var endPoint = new Uri("http://localhost:8080/jasperserver/rest_v2/users/" 
      + HttpUtility.UrlEncode(userName));
Hendricks answered 31/3, 2015 at 11:52 Comment(2)
I actually looked at this post before posting mine. I am slightly confused about- NetworkCredential(string, string) function. It takes 2 input strings but I have to pass only 1 string userid. It doesn't seem to have an overload which accepts 1 input parameter.Jackass
See my edit - the NetworkCredential section is a bit misleading, the point was that the error does not have to be caused by the specified portHendricks
J
2

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

var endPoint = new Uri(string.Concat("http://localhost:8080/jasperserver/rest_v2/users/", userName));
Jackass answered 11/4, 2015 at 10:24 Comment(0)
R
0

Make sure you're using something like https://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode%28v=vs.110%29.aspx on the userName param before you concat it to the URL.

Revenuer answered 31/3, 2015 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.