【问题标题】:System.UriFormatException: Invalid URI : The URI is EmptySystem.UriFormatException:无效的 URI:URI 为空
【发布时间】:2015-05-19 04:32:58
【问题描述】:

我使用的 URL 在我将其放入 chrome 时有效,但在我的 VS C# 代码中使用相同的 URL(公共异步静态任务)

using (var client = new HttpClient(handler))  {    
client.BaseAddress = new Uri(url);    ----------> Error

....

Method threw exception: 
System.AggregateException: One or more errors occurred. ---> System.UriFormatException: Invalid URI: The URI is empty.
Result StackTrace:  
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
   at System.Uri..ctor(String uriString)

我确定 URL 没有格式错误,所以我无法弄清楚这个错误的原因可能是什么。感谢您的帮助。

【问题讨论】:

  • 请在您的问题中添加网址
  • 确保您在 url(http 或 https)中包含协议 - 没有它,Uri 创建将失败。
  • @Novak007 在调试时,你看到url 变量包含你所期望的吗?我怀疑它里面有空字符串。
  • @Novak007 你真的在调试你的代码吗?你应该这样做,这样你就不会误导人们。我认为发布您的实际代码可能会有所帮助

标签: c# .net visual-studio visual-studio-2013


【解决方案1】:

您在 cmets 中发布了您的 URL 看起来像 services.odata.org/Northwind/Northwind.svc/…。您缺少 URL 中的协议,并且 Uri 构造函数需要它。尝试将其更改为:

var url = "http://services.odata.org/Northwind/Northwind.svc/Employees?$filter=minute%28BirthDate%29";
client.BaseAddress = new Uri(url);

注意http:// - 那是协议。您可能还必须从 URL 中删除参数,但您可以快速测试。

【讨论】:

  • 其实我用过但还是报错。
  • 我通过创建一个键值对将我的 url 放在 app.config 中,然后使用 configuration manager.appsettings 在一个类中访问它。我想这是访问它的好方法,但是空 url 意味着我不能,所以请你提示一下可能出了什么问题
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 2011-10-05
相关资源
最近更新 更多