传递viewstate / event验证令牌;引起问题的特殊字符

时间:2012-10-25 15:46:21

标签: c# asp.net .net .net-4.0

我有一个viewstate和eventvalidation令牌,我试图通过我的应用程序传递。

我遇到的问题是我将这些信息传递给:

string.Format("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE={0}&__EVENTVALIDATION={1}&{2}", viewstate, eventvalidation, request)

eventvalidation在其中有一个加号(+),它会导致字符串的cat,而不是显示文字字符。有什么想法可以防止这种情况发生吗?

以下是我执行请求的代码:

WebRequest req = WebRequest.Create(url);
//Here Request is working properly; the EVENTVALIDATION token has the + sign in it.
byte[] send = Encoding.Default.GetBytes(request);
// I think after I convert it to byte[], it is doing something bad to the EVENTVALIDATION token.
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = send.Length;

Stream sout = req.GetRequestStream();
sout.Write(send, 0, send.Length);
sout.Flush();
sout.Close();

WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string returnvalue = sr.ReadToEnd();

return returnvalue;

4 个答案:

答案 0 :(得分:2)

我使用了Uri.EscapeDataString,这就行了!

答案 1 :(得分:1)

使用String.Concat()

String.Concat("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=", 
           viewstate,"&__EVENTVALIDATION=", eventvalidation,"&", request);  

答案 2 :(得分:0)

尝试使用HTMLEncode取走+。你把它放在一个HiddenField中吗?

String.Format("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE={0}&__EVENTVALIDATION={1}&{2}", Server.HtmlEncode(viewstate), Server.HtmlEncode(eventvalidation), Server.HtmlEncode(request)))

http://msdn.microsoft.com/en-us/library/w3te6wfz.aspx

答案 3 :(得分:0)

HttpUtility.UrlEncode会为您指定的内容类型正确编码值。有关详细信息,请参阅this answer

相关问题