【问题标题】:System.Uri.CreateThis returning UriFormatException - The URI is empty ... but it isn'tSystem.Uri.CreateThis 返回 UriFormatException - URI 是空的......但它不是
【发布时间】:2015-05-05 23:27:27
【问题描述】:

今天出现了几次奇怪的问题。

我有一个 WPF 应用程序,它向服务器发送一些帖子。我正在使用 HttpWebRequest.Create 执行此操作。在这种情况下,正在使用的 URL 是硬编码的。示例:

Dim Request As HttpWebRequest = HttpWebRequest.Create("https://www.google.com/")

这就是我能提供的所有代码,异常是从 HttpWebRequest.Create 方法中生成的。据我所知,正在运行 .Net 4.0。

我也使用 C#,所以如果你有一个建议,那就是你的“母语”,那就拍吧。

我有几个问题,所以我想我会添加到周围的其余代码中,但我在上面发布的行是异常直接来自的行。您将在下面的上下文中看到它,但它不是很有帮助。

Private Function edatRequest() As CookieCollection
    Dim Request As HttpWebRequest = HttpWebRequest.Create("https://www.google.com/")
    With Request
        .AllowAutoRedirect = False
        .Method = "POST"
        .Accept = "*/*"
        .Headers.Add("Accept-Encoding:gzip,deflate")
        .Headers.Add("Accept-Language:en-US,en;q=0.8")
        .KeepAlive = True
        .CookieContainer = Me.MyCredentials.MyCookies
        .ContentLength = 0
        .ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
        .Host = "https://www.google.com/"
        .Headers.Add("Origin:https://www.google.com/")
        .Referer = "https://wwww.google.com/"
        .UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"
    End With

    Dim Response As HttpWebResponse = Request.GetResponse
    Dim ResponseReader As New StreamReader(Response.GetResponseStream)
    Dim ResponseText As String = ResponseReader.ReadToEnd

    Dim ReturnCookies As New CookieContainer
    ReturnCookies.Add(Response.Cookies)
    Return ReturnCookies
End Sub

编辑:从我们的错误通知器中添加一些堆栈跟踪信息:

The application has encountered an unhandled exeption and must
end.  Please forward the following information to (our 
department):  System.UriFormatException: Invalid URI:  The URI is
empty.
    at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind
uriKind)
    at System.Uri..ctor(String uriString)
    at System.Net.WebRequest.Create(String requestUriString)
    at AutoLib.Automation.edatRequest()
    at AutoLib.Automation.LogIn()
    at AutoLib.Automation.Submit(Request1 Request) <- not actually a 1 - generic sub
    at AutoTool.MainWindow.GetAuto()
    at AutoTool.MainWindow.Lambda$_21()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
preserveSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()

你去。

【问题讨论】:

  • 在 C# 中 HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create("https://www.google.com/"); 应该可以编译并正常工作。不太清楚发生了什么。
  • 贴出的代码没有问题。这是给你一个例外的行吗?
  • 你能分享你的确切代码吗,你可以在分享代码的同时将域名更改为xyz。
  • 这是导致异常的确切行,具有不同的 url。我真的不认为这是代码中的错误,更像是.Net中可能随机出现的一些问题。我已要求用户重新启动。同样,这是导致问题的确切行,逐字逐句(网址除外)。
  • 听起来问题在于传递给Create的字符串。找出哪些地址引发了异常并报告回来。还有什么是内部异常——可能会泄露它。

标签: c# wpf vb.net httpwebrequest


【解决方案1】:

我遇到了同样的问题,它是由作为 URL 传递的 UTF8 字符串引起的。

我不得不将字符串复制到记事本++,将其转换为十六进制,发现在 http 内容之前有 E2 80 8B 字符开始字符串。

此前缀是 UTF8 的 unicode 指示符。

重新输入整个 URL,它应该会消失。注意,删除整个字符串并重新输入,不要只修改字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 2018-09-25
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多