2

Here's an odd one that has come up a couple of times today.

I have a WPF app that does some posts to a server. I'm doing this using HttpWebRequest.Create. In this instance, the URL that is being used is hard-coded. Example:

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

That's really all the code I can offer, the exception is being generated from within the HttpWebRequest.Create method. Running .Net 4.0 as far as I'm aware.

I do C# as well, so if you have a suggestion and that is your "native language" then shoot.

I've had a few asks so I thought I'd add to the rest of the surrounding code, but the line I've posted above is the line the exception is coming directly from. You'll see it in context below, but it isn't very helpful.

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

EDIT: Adding some stack trace information from our error notifier:

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

There you go.

MattB
  • 2,203
  • 5
  • 26
  • 48
  • in C# `HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create("https://www.google.com/");` should compile and work fine. Not really sure what is going on. – Habib Mar 04 '15 at 19:53
  • 2
    There is nothing wrong with the posted code. Is this the line that's giving you an exception? – Rufus L Mar 04 '15 at 19:54
  • Can u share your exact code, you can obviously change domain names to xyz while sharing the code. – Gaurav Sharma Mar 04 '15 at 20:00
  • This is the exact line, with a different url, that is causing the exception. I don't really think it's an error in the code, more like some issue in .Net that maybe comes up randomly. I've asked the user to restart. Again, this is the exact line, verbatim (except the url) that is causing the issue. – MattB Mar 04 '15 at 20:03
  • Sounds like the issue lies within the string being passed to `Create`. Figure out what addresses throw the exception and report back. Also what is the inner exception--that may give it away. – Kcvin Mar 04 '15 at 20:37
  • If you can add the stacktrace to the post (renaming/obfuscating your lines of code as desired) that would help. Looking at the [.NET source](http://referencesource.microsoft.com/#System/net/System/URI.cs,1989) for WebRequest.Create shows that it's really only for a null/zero-length string that you'll get a "URI is empty" exception. – NextInLine Mar 04 '15 at 20:40
  • @NextInLine Posted some more info for you. The string is just a hard coded string. I don't really think the error is on my end, but if there is something I can do to prevent it I'd like to do so. Seems to be an error in .Net but I don't know for sure. User has restarted and hasn't reported the error again - yet. Happened a few times earlier today. – MattB Mar 04 '15 at 20:52
  • @MattB - a method that is as wide-used as this is pretty unlikely to have a bug in .NET. I'd sooner suspect memory corruption on the user's machine, especially if it is intermittent. You could also try getting them to upgrade their .NET version if it persists to see if that fixes it. – NextInLine Mar 04 '15 at 21:12
  • How are you generating the address that is being used in place of google.com? – Kcvin Mar 04 '15 at 21:23
  • @NETscape It is hard coded. – MattB Mar 04 '15 at 22:19
  • @NextInLine Correct, pretty unlikely. I should have been more specific - I am guessing something is corrupted or something happened on his machine rather than a bug in .Net itself. I guess we'll just see if it continues to happen. It's annoying though. I don't want to have to wrap everything in Try catch, but I'll have to rethink how I'm doing some of this to give the user a chance to recover rather than booting them. That's pretty rare though. – MattB Mar 04 '15 at 22:22
  • The issue is continuing after restart. Any ideas on how I could get closer to tracking down the root cause of this issue? – MattB Mar 06 '15 at 17:57

1 Answers1

0

I had the same problem and it was caused by an UTF8 string being passed as the URL.

I had to copy the string to notepad++, convert it to hex and found out there was E2 80 8B characters starting the string before the http stuff.

This prefix is the unicode indicator for UTF8.

Retype the whole URL and it should go. Caution, erase the whole string and retype it, do not merely modify the string.

Stécy
  • 11,951
  • 16
  • 64
  • 89