无效的 URI:在 C# 中指定的端口号无效时指定 URL,其中包含端口号,例如 http://localhost:8080/jasperserver/rest
Invalid URI: Invalid port specified in C# on specifying URL with port number in it like http://localhost:8080/jasperserver/rest
我创建了一个简单的函数来执行 Http PUT 请求 -
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";
}
当我执行这个时,我在行 -
处得到一个异常 "Invalid URI: Invalid port specified"
var endPoint = new Uri("http://localhost:8080/jasperserver/rest_v2/users/"+userName);
有什么解决办法吗? URL 的 localhost:8080 部分有问题吗?
看看这个问题Invalid URI: Invalid port specified when url has more than one colon
您是否检查过您的用户名中是否存在可能导致问题的字符?
评论后编辑 - 我引用这个问题的原因是因为 'Invalid Port' 错误的发生不是因为实际端口错误,而是因为 URL 中的其他无效字符。验证用户名是否正确编码将防止此问题。
var endPoint = new Uri("http://localhost:8080/jasperserver/rest_v2/users/"
+ HttpUtility.UrlEncode(userName));
在将用户名参数连接到 URL 之前,请确保在用户名参数上使用类似 https://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode%28v=vs.110%29.aspx 的内容。
我使用 string.Concat()
方法修正了这个错误:-
var endPoint = new Uri(string.Concat("http://localhost:8080/jasperserver/rest_v2/users/", userName));
我遇到了这个问题,发现是我的粗心导致了这个问题。我在创建 HttpRequestMessage 实例时遇到了问题,如下所示。
var request = new HttpRequestMessage(new HttpMethod("PUT"),
$"{_nextcloudConfigurationProvider.NextcloudBaseUrl}{FileConstants.ApiUploadFile.Replace("{UserName}", UserName)}");
问题是我在第二个常量之前漏掉了一个斜杠“/”。它是 remote.php/dav/files/{UserName}/{directoryName}/
而不是 /remote.php/dav/files/{UserName}/{directoryName}/
。添加斜线为我解决了这个问题。
只是分享给像我这样想念愚蠢事情的人。
我创建了一个简单的函数来执行 Http PUT 请求 -
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";
}
当我执行这个时,我在行 -
处得到一个异常 "Invalid URI: Invalid port specified"var endPoint = new Uri("http://localhost:8080/jasperserver/rest_v2/users/"+userName);
有什么解决办法吗? URL 的 localhost:8080 部分有问题吗?
看看这个问题Invalid URI: Invalid port specified when url has more than one colon
您是否检查过您的用户名中是否存在可能导致问题的字符?
评论后编辑 - 我引用这个问题的原因是因为 'Invalid Port' 错误的发生不是因为实际端口错误,而是因为 URL 中的其他无效字符。验证用户名是否正确编码将防止此问题。
var endPoint = new Uri("http://localhost:8080/jasperserver/rest_v2/users/"
+ HttpUtility.UrlEncode(userName));
在将用户名参数连接到 URL 之前,请确保在用户名参数上使用类似 https://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode%28v=vs.110%29.aspx 的内容。
我使用 string.Concat()
方法修正了这个错误:-
var endPoint = new Uri(string.Concat("http://localhost:8080/jasperserver/rest_v2/users/", userName));
我遇到了这个问题,发现是我的粗心导致了这个问题。我在创建 HttpRequestMessage 实例时遇到了问题,如下所示。
var request = new HttpRequestMessage(new HttpMethod("PUT"),
$"{_nextcloudConfigurationProvider.NextcloudBaseUrl}{FileConstants.ApiUploadFile.Replace("{UserName}", UserName)}");
问题是我在第二个常量之前漏掉了一个斜杠“/”。它是 remote.php/dav/files/{UserName}/{directoryName}/
而不是 /remote.php/dav/files/{UserName}/{directoryName}/
。添加斜线为我解决了这个问题。
只是分享给像我这样想念愚蠢事情的人。