簡體   English   中英

C# 無效 URI:指定的端口無效

[英]C# Invalid URI: Invalid port specified

new Uri("https://api-dev.xxx.com.tr:9280​/order​/02357063/status")' 
threw an exception of type 'System.UriFormatException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233033
    HelpLink: null
    InnerException: null
    Message: "Invalid URI: Invalid port specified."
    Source: "System.Private.Uri"
    StackTrace: 
   "at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)\r\n
    at System.Uri..ctor(String uriString)"
TargetSite: {Void CreateThis(System.String, Boolean, System.UriKind, System.UriCreationOptions ByRef)}

您的 URL 字符串包含零寬度空格字符(Unicode 字符代碼 U+200B)。 您看不到它們,因為它們沒有/零寬度。 [1]

具體來說,零寬度空格字符位於 url 字符串中的這些位置,用星號 (*) 標記:

https://api-dev.xxx.com.tr:9280*/order*/02357063/status

我不知道您是如何設法將這些字符放入 url 字符串中的,但具體來說,這些零寬度空格字符中的第一個是您遇到的錯誤的原因。 您會注意到,第一個零寬度空格字符緊跟在端口號之后,這對 URL 無效。 URL 中的權限后允許的唯一字符(在您的情況下,權限是服務器名稱和端口號)是/表示路徑跟隨, ? 表示后面跟着一個查詢,或者#表示后面跟着一個 URL 片段。

訂單后的第二個零寬度空格字符正好位於 url 路徑的中間——盡管與您遇到的錯誤無關——也是有問題的,因為它很可能會阻止服務器正確解析 url 路徑。

The solution: Type the URL string manually anew (don't do copy&paste; type the url string completely manually), because this way you should not get unwanted zero-width space characters or other nasties in your URL string simply because you can't直接輸入這樣的字符(不支持 ALT+Numpad 技術......)。


[1]您可以借助 Notepad++ 使這些零寬度空格字符可見,例如:在 Notepad++ 中打開一個新的空 UTF-8 或 UTF-16 文檔。 然后將問題中的 URL 字符串復制並粘貼到 Notepad++ 中的該文本文檔中。 然后將 Notepad++ 中文本文檔的編碼改為 ANSI,以前不可見的零寬度空格字符變為可見的問號或一堆“垃圾”字符。

暫無
暫無

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

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