簡體   English   中英

無效的URI:在指定帶有端口號的URL時在C#中指定的無效端口,如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";
    }

當我執行此操作時,我得到一個異常“無效的URI:指定了無效的端口” -

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

有什么想法解決這個問題? 是localhost的問題:網址的8080部分?

看看這個問題無效的URI:當url有多個冒號時指定的端口無效

您是否檢查過用戶名是否存在可能導致問題的字符?

評論后編輯 - 我引用此問題的原因是因為發生'無效端口'錯誤不是因為實際端口錯誤,而是因為URL中的其他無效字符。 驗證用戶名是否已正確編碼可以防止出現此問題。

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

我使用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}/ 添加斜杠為我解決了問題。

只是為那些想念傻事的人分享這個,就像我一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM