gpt4 book ai didi

c# - 无效的 URI : Invalid port specified when url has more than one colon

转载 作者:行者123 更新时间:2023-11-30 14:13:01 24 4
gpt4 key购买 nike

我正在使用 dnsdynamic.org 指向我在家里托管的个人网站。我的家庭 ip 会每月频繁更改一次。 dnsdynamic.org 提供了一个 web 方法调用来更新 ip。

https://username:password@www.dnsdynamic.org/api/?hostname=techno.ns360.info&myip=127.0.0.1

当我通过浏览器调用它时,它工作得很好。当我尝试通过 C# 代码调用它时,它会抛出以下异常。

Invalid URI: Invalid port specified.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Net.WebRequest.Create(String requestUriString)

因为 url 中有一个冒号,所以它看起来像 system.uri 尝试将我的密码解析为整数。

我尝试在 .org 之后添加一个端口

https://username:password@www.dnsdynamic.org:443

运气不好。

谁能告诉我如何解决这个错误。

我尝试在创建 uri 时使用“不要转义”和“转义”选项,但仍然无法正常工作。

var ri = new Uri("https://username:password@www.dnsdynamic.org/api/?hostname=techno.ns360.info&myip=127.0.0.1",true);

编辑:看起来是用户名导致了问题。我的用户名是我的电子邮件地址。我的电子邮件地址以 *.com 结尾。所以 uri 正在尝试解析我的密码。

仍然没有找到解决这个问题的方法。我无法更改我的用户名,因为 dnsdyanmic.org 使用电子邮件地址作为用户名。

最佳答案

如果您使用 HttpWebRequest 进行请求,则不应在 URI 中包含凭据(如您所知,它是无效的)。

您应该改为将凭据添加到 HttpWebRequest 并让它处理传递它们:

// Only broke this up for readability
var uri = new Uri("https://www.dnsdynamic.org/api/?hostname=techno.ns360.info" +
"&myip=127.0.0.1", true);
var cache = new CredentialCache();
cache.Add(uri, "Basic", new NetworkCredential("username", "password"));

var request = WebRequest.Create(uri);
request.Credentials = cache;

关于c# - 无效的 URI : Invalid port specified when url has more than one colon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15534034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com