asp.net post 没有事件响应!高手,专家来

libinlink 2010-03-29 10:40:53
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:63527/AutoPost/Default.aspx";
string data = "TextBox1=red&TextBox2=empty&Button1=clicked";

string vs = "/wEPDwULLTExNTc2NTI3OTlkZJzeeKml7aOumj+ZNNWKyehf/JG7";
string ev = "/wEWBALPrra8BALs0bLrBgLs0fbZDAKM54rGBqhY2BGeniakCaDh0LY7ra9v+V6t";

vs = HttpUtility.UrlEncode(vs);
ev = HttpUtility.UrlEncode(ev);

data += "&__VIEWSTATE=" + vs + "&__EVENTVALIDATION=" + ev;
byte[] buffer = Encoding.Unicode.GetBytes(data);

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = buffer.Length;

Stream reqst = req.GetRequestStream();
reqst.Write(buffer, 0, buffer.Length);
reqst.Flush();
reqst.Close();

Console.WriteLine(" send the request over.");

HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream resst = res.GetResponseStream();
StreamReader sr = new StreamReader(resst);

Console.WriteLine("\nGrabbing HTTP response:\n");
Console.WriteLine(sr.ReadToEnd());
sr.Close();
resst.Close();

Console.WriteLine("Done.3");
Console.Read();


}
}
我给一个页面发送了post请求,但是没有响应按钮Button1的事件,Textbox1的value值也没被赋予red.
我发送的post消息已经包含了正确的 "&__VIEWSTATE=" + vs + "&__EVENTVALIDATION=" + ev;
为什么我捕获的响应字符串,好像是第一次加载页面的值


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
auto post
</title></head>
<body>
<h3>color commenter</h3>
<form name="form1" method="post" action="Default.aspx" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTExNTc2NTI3OTlkZJzeeKml7aOumj+ZNNWKyehf/JG7" />

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBALPrra8BALs0bLrBgLs0fbZDAKM54rGBqhY2BGeniakCaDh0LY7ra9v+V6t" />
<p>
enter color:</p>
<input name="TextBox1" type="text" id="TextBox1" />
<p>
my commnet:</p>
<input name="TextBox2" type="text" id="TextBox2" />
<p>
<input type="submit" name="Button1" value="Send" id="Button1" /></p>
</form>
</body>
</html>
为什么Textbox1没有被赋予red,也没响应Button1的Click事件!

我使用的vs2008. .net framework3.5建立的测试网页Default.aspx!

...全文
176 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cpp2017 2010-03-29
  • 打赏
  • 举报
回复
  string url = " http://localhost:1925/WebForm1.aspx";
string data = "TextBox1=123&TextBox2=bbbbbbbb&Button1=Button";

string vs = "/wEPDwULLTE1OTg1NDYyNDZkZCGOa/w0BH7Cs9LGH2qS3FL67tzp";
string ev = "/wEWBAK18471BwLs0bLrBgKM54rGBgLs0fbZDHqUBuWM1RQZNNSGQxX0Vz3+amFh";
vs = HttpUtility.UrlEncode(vs);
ev = HttpUtility.UrlEncode(ev);

data += "&__VIEWSTATE=" + vs + "&__EVENTVALIDATION=" + ev;

CookieContainer cook = new CookieContainer();
string HTML = getHTMLByUrlCook(url, ref cook, "POST", data, true, System.Text.Encoding.UTF8);
Console.Write(HTML);



public string getHTMLByUrlCook(string url, ref System.Net.CookieContainer cook, string sMethod, string Param, bool bAutoRedirect, System.Text.Encoding ecode)
{
sMethod = sMethod.ToUpper();
sMethod = sMethod != "POST" ? "GET" : sMethod;
string res = "";
HttpWebRequest re = (HttpWebRequest)HttpWebRequest.Create(url);
re.CookieContainer = cook; // attach the cook object
re.Method = sMethod;
re.AllowAutoRedirect = bAutoRedirect;
re.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; MyIE2; .NET CLR 1.1.4322)";



re.Referer = url;
if (sMethod == "POST") // Post data to Server
{
re.ContentType = "application/x-www-form-urlencoded";
Byte[] b = ecode.GetBytes(Param);
re.ContentLength = b.Length;
try
{
Stream oSRe = re.GetRequestStream();
oSRe.Write(b, 0, b.Length);
oSRe.Close();
oSRe = null;
}
catch (Exception)
{
re = null;
return "-1";
}
}

HttpWebResponse rep = null;
Stream oResponseStream = null;
StreamReader oSReader = null;
try
{
rep = (HttpWebResponse)re.GetResponse();

oResponseStream = rep.GetResponseStream();
oSReader = new StreamReader(oResponseStream, ecode);
res = oSReader.ReadToEnd();
}
catch (System.Net.WebException e)
{
//res ="-1";

res = e.ToString();
}

if (rep != null)
{
rep.Close();
rep = null;
}
if (oResponseStream != null)
{
oResponseStream.Close();
oResponseStream = null;
}

if (oSReader != null)
{
oSReader.Close();
oSReader = null;
}
re = null;

return res;

}




libinlink 2010-03-29
  • 打赏
  • 举报
回复
我用控制台程序给aspx页面(Default.apsx)发送post消息,
请求的页面Default是我自己建立的,Button的Click事件写了。
专家帮忙呀!
丰云 2010-03-29
  • 打赏
  • 举报
回复
自己单步调试吧。。。
Button1的Click事件在那里呢????
caofan520 2010-03-29
  • 打赏
  • 举报
回复
string data = "TextBox1=red&TextBox2=empty&Button1=send";
Button1的值是Send
或者抓个包看下少传了哪些数据
ahhisoft 2010-03-29
  • 打赏
  • 举报
回复
form中所有的表单元素都要传值。包括ViewState和Button。

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧