tags:

views:

57

answers:

1

I am trying to throw together a screen scraper and keep getting invalid viewstate issues. It appears that during the System.Net.WebClient download of data or the System.Text.UTF8Encoding.Default.GetString call to convert the byte array returned by the WebClient DownloadData call to a string - that strings which match url character codes are being converted.

ie

Url encoded characters strings like %2B are being converted to their normal characters (+ for %2B).

Is this happening in the WebClient class? Is it the way I am converting the byte array to a string?

EDIT:

Based on suggestions I tried changing to the DownloadString call from the WebClient class and the resulting string has converted the character codes to the specific character so it appears WebClient is the culprit.

EDIT 2:

Solved. By making a call to System.Web.HttpUtility.UrlEncode I was able to convert the + back to %2B before sending the viewstate string back up to the server in subsequent requests. I am still at a loss as to where and why the problem was occurring but the server was expecting a viewstate string that contained ...%2B... and was getting ...+... and determining the viewstate to be invalid and throwing the exception. Kudos to Jon & Henk for forcing me to rethink my assumptions.

+2  A: 

If you use System.Text.UTF8Encoding.Default then you're not using UTF-8 - you're using the default encoding for the system. It's equivalent to Encoding.Default, but in a more confusing form. Use Encoding.UTF8 to get a UTF-8 encoding... or use WebClient.DownloadString as Henk suggested.

On the other hand, it's not clear what you're trying to download. If you're trying to download geuinely binary data then you shouldn't be trying to convert it to a string at all.

It would help if you would clarify you question - try to provide a lot more context about what's making the requests, what's having problems, etc.

Jon Skeet
Appreciate the tip but my original problem persists even after this change.
keithwarren7
@keithwarren7: You still haven't actually explained what the context is.
Jon Skeet