From: some one on
I have kind of wired problem, I using httpwebrequest to post form data to
server , in the GetResponse stage a WebException occurred, after tracing the
actual error that occurs on the server, I found the follows.



Invalid length for a Base-64 char array.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.


Exception Details: System.FormatException: Invalid length for a Base-64 char
array.

Source Error:

An unhandled exception was generated during the execution of the current web
request. Information regarding the origin and location of the exception can
be identified using the exception stack trace below.
Stack Trace:

[FormatException: Invalid length for a Base-64 char array.]\r\n
System.Convert.FromBase64String(String s) +0\r\n
System.Web.UI.LosFormatter.Deserialize(String input) +25\r\n
System.Web.UI.Page.LoadPageStateFromPersistenceMedium()
+101\r\n\r\n[HttpException (0x80004005): Invalid_Viewstate tClient IP:
202.43.3.233\r\n\tPort: 1358\r\n\tUser-Agent: Mozilla/4.0 (compatible; MSIE
6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 1.0.3705)
ViewState: dDwtMTU1Mjc5NDYyMjt0PDtsPGk8MD47PjtsPHQ8O2w8aTwwPjtpPDI
O2k8ND47PjtsPHQ8O2w8aTwwPjs O2w8dDw7bDxpPDA O2k8MT47aTwzPjtpPDQ
O2k8NT47aTw2PjtpPDc O2k8OD47PjtsPHQ8cDxsPGlubmVyaHRtbDs
O2w8V0FFQyAtIFNlYXJjaDs
Pjs7Pjt0PHA8bDxpbm5lcmh0bWw7PjtsPA0KCQkJQGltcG9ydCB1cmwoL2Nzcy9XQUVDU3ViU3R5bGUuY3NzKVw7CQ0KCQkJQGltcG9ydCB1cmwoL2Nzcy93YWVjTW9kdWxlLmNzcylcOwkJCQkNCgkJQGltcG9ydCBVUkwoL2Ntbi9jc3MvcG9ydGFsbWFzdGVyLmNzcylcOw0KOz4
Ozs
O3Q8cDxsPGNvbnRlbnQ7PjtsPCBTdGF0ZSBFbGVjdGlvbnMsV2VzdGVybiBBdXN0cmFsaWFuIEVsZWN0b3JhbCBDb21taXNzaW9uLCBXQSBFbGVjdGl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wPjs
O2w8dDxwPGw8aW5uZXJodG1sOz47bDxcPGEgaHJlZj0iaHR0cDovL3dhZWMyNS5hbC5pYmMuY29tLmF1LzEvMzIzMi83My9vdGhlcl9sYW5ndWFnZXMucG0iXD5PdGhlciBMYW5ndWFnZXNcPC9hXD5cPGEgaHJlZj0iaHR0cDovL3dhZWMyNS5hbC5pYmMuY29tLmF1LzEvOTMvNzMvc2l0ZW1hcC5wbSJcPlNpdGUgTWFwXDwvYVw
XDxhIGhyZWY9Imh0dHA6Ly93YWVjMjUuYWwuaWJjLmNvbS5hdS8xLzMxNzAvNzMvZ2xvc3NhcnkucG0iXD5HbG9zc2FyeVw8L2FcPlw8YSBocmVmPSJodHRwOi8vd2FlYzI1LmFsLmliYy5jb20uYXUvMS85MC83My9saW5rcy5wbSJcPkxpbmtzXDwvYVw
XDxhIGhyZWY9Imh0dHA6Ly93YWVjMjUuYWwuaWJjLmNvbS5hdS8iXD5IZWxwXDwvYVw
XDxhIGhyZWY9Imh0dHA6Ly93YWVjMjUuYWwuaWJjLmNvbS5hdS8xLzg5LzczL2xlZ2FsLnBtIlw
TGVnYWxcPC9hXD47Pj47Oz47Pj47Pj47Pj47Pj47Pj47Pj47Pj47Pg==t
Http-Referer: Path: /Default.aspx.]
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +441\r\n
System.Web.UI.Page.LoadPageViewState() +18
System.Web.UI.Page.ProcessRequestMain() +447
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET
Version:1.1.4322.2032.


AS you see the view state is quite large , but there is no proxey and it
work fine on te actual application witout simulating the request via
httpwebrequest.
Here you are some code :


HttpWebRequest objHttpWebRequest = (HttpWebRequest) WebRequest.Create (
_strUrl );
objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
objHttpWebRequest.Headers.Add("Cache-Control","no-cache");
if (_objCookieCollection["ASP.NET_SessionId"]!=null)
objHttpWebRequest.Headers.Add("Cookie",_objCookieCollection["ASP.NET_SessionId"].Value);
objHttpWebRequest.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 1.0.3705)";
StringBuilder sb = new StringBuilder ( );
int intNumKeys = _arrayListFormFields.Count;
int i = 0;
foreach ( FormField__ objFormField in _arrayListFormFields )
{
string strKeyName = objFormField.Key;
string strKeyValue = objFormField.Value;
string test="";
sb.Append ( strKeyName );
sb.Append ( "=" );
sb.Append (strKeyValue );
if ( i<intNumKeys-1 )
{
sb.Append ( "&" );
}
i++;
}

CookieContainer objCon= new CookieContainer();
if (_objCookieCollection!=null)
objCon.Add(_objCookieCollection);
objHttpWebRequest.CookieContainer=objCon;
string strPostData = sb.ToString ( );
ASCIIEncoding asciiEncoding = new ASCIIEncoding ( );
byte[] byteArrayPostData = asciiEncoding.GetBytes ( strPostData );
objHttpWebRequest.ContentLength = strPostData.Length;
Stream objRequestStream = objHttpWebRequest.GetRequestStream ( );
objRequestStream.Write( byteArrayPostData, 0, byteArrayPostData.Length );
objRequestStream.Close();
HttpWebResponse objWebResponse = null;
objWebResponse =(HttpWebResponse) objHttpWebRequest.GetResponse ( ); // The
error occurs here!!!!


Well I know that the problem is something is messing up the viewstate, but
what?

Any one has an answer?



From: albert braun on
the ViewState string in the dump appears to have a "t" at the end. i
would have expected "=" to be the last character. that "t" looks
suspicious to me.

From: AL on
this t is a typo, just ignore it.

"albert braun" wrote:

> the ViewState string in the dump appears to have a "t" at the end. i
> would have expected "=" to be the last character. that "t" looks
> suspicious to me.
>
>
From: albert braun on
in that case, i'm suspicious of this line:

byte[] byteArrayPostData = asciiEncoding.GetBytes ( strPostData );

maybe you want to urlencode strPostData first?
or maybe you want to use a different encoding to get the bytes --
perhaps UTF-8 ?
or both?

that's my guess. i'd try to experiment with those things.