System.Uri.CreateThis返回UriFormatException - URI为空......但它不是

时间:2015-03-04 19:49:34

标签: c# wpf vb.net httpwebrequest

这是一个奇怪的问题,今天出现了几次。

我有一个WPF应用程序可以对服务器发布一些帖子。我正在使用HttpWebRequest.Create这样做。在这种情况下,正在使用的URL是硬编码的。例如:

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

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

我也做C#,所以如果你有一个建议,那就是你的#34;母语"然后开枪。

我有一些要求所以我认为我会添加到周围代码的其余部分,但我上面发布的行是异常直接来自的行。您将在下面的上下文中看到它,但它并不是非常有用。

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()

你去。

1 个答案:

答案 0 :(得分:0)

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

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

此前缀是UTF8的unicode指示符。

重新输入整个网址,它应该去了。注意,擦除整个字符串并重新键入它,不要只修改字符串。