0

我有一个视图状态和事件验证令牌,我试图通过我的应用程序传递。

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

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

4 回答 4

2

我使用了 Uri.EscapeDataString 并且成功了!

于 2012-10-25T17:13:51.163 回答
1

采用String.Concat()

String.Concat("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=", 
           viewstate,"&__EVENTVALIDATION=", eventvalidation,"&", request);  
于 2012-10-25T15:51:43.973 回答
0

尝试使用 anHTMLEncode去掉 +。你把它放在一个 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

于 2012-10-25T16:01:15.783 回答
0

HttpUtility.UrlEncode将为您指定的内容类型正确编码值。有关更多详细信息,请参阅此答案

于 2012-10-25T17:08:11.353 回答