关于使用HttpWebRequest自动Post问题

彖爻之辞 2011-11-03 09:41:42
关于使用HttpWebRequest自动Post问题
网站有登录页面和数据处理页面。登录页面负责处理用户登录,数据处理页面负责加入数据到数据库中。
现在的问题是,可以通过WebRequest去Post用户名和密码进入登录页面[如:http://192.168.1.20/Default.aspx],成功!
HttpWebResponse返回了数据处理页面[如:http://192.168.1.20/InsertData.aspx]
现在开始向这个页面发送Post数据。但是我怎么去做呢?
如果直接向[如:http://192.168.1.20/InsertData.aspx]页面Post数据的话,它会跳转到登录页面要求登录。
两个页面是自己做的。
在做的Post过程中加入了viewState和EventValidation的内容了。
请高手帮忙啊!已经两天了。
...全文
498 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shinni987654 2013-03-26
  • 打赏
  • 举报
回复
hwResquest.ContentLength = postData.Length; hwResquest.Method = "POST"; hwResquest.CookieContainer = cookContainer; 有些不明白的,到这里 cookContainer还是空值吧
if (cookieheader.Equals(string.Empty)){cookieheader = hwResquest.CookieContainer.GetCookieHeader(new Uri(uriString));}
if (!cookieheader.Equals(string.Empty) && cookieheader.Length != 0){hwResquest.CookieContainer.SetCookies(new Uri(uriString), cookieheader);cookContainer = hwResquest.CookieContainer;}
到这里cookContainer有值吗,如果有post请求是否完成,菜鸟看不懂,这样是带着cookies post吗
Bullatus 2013-03-15
  • 打赏
  • 举报
回复
用CookieContainer啊。 CookieContainer container; 登录时 request.CookieContainer=container; post数据时带着container就可以了
我是老姚 2013-03-15
  • 打赏
  • 举报
回复
第2页在第1页提交成功以后再提交第2页, 第1页提交的目的是要取到SESSIONID //初始化HttpWebResponse对象hwResponse为了获得服务器应答 HttpWebResponse hwResponse = (HttpWebResponse)hwResquest.GetResponse(); 在这之后要找到服务器的SESSIONID,类似下面的方法。 hwResponse.Cookies["SessionID"]//不同的服务器存SessionID的Cookie名称不一样,可以使用HttpWatch或浏览器的开发者工具追踪一下,很容易就可以知道。 存下来后在第2个页提交的时候要把这个作为Cookie的内容之一一起提交,否则服务器还是认为你没有登录。
我是老姚 2013-03-15
  • 打赏
  • 举报
回复
//先提出一个提交第1页的C#程序

//定义变量
#region
string postString = string.Empty;        //Post字串
string stringResponse = string.Empty;    //接收服务器应答字串
byte[] postData;                         //Post Data
ASCIIEncoding encoding = new ASCIIEncoding();             //代码转换时使用
string cookieheader = string.Empty;                       //Cookies Header
//CookieCollection cookieCollection;                        //创建CookieCollection对象cookieCollection
CookieContainer cookContainer = new CookieContainer();  //创建CookieContainer对象cookieContainer
#endregion

//第1页Login
#region
//初始化变量和POST串
#region
  
#region
string urlstring = "http://192.168.104.35:8001/";
string ViewState = "/wEPDwUKMTEyMTc3MTQwNmRkLjlNkqKUMAkS3auwl+zhtnVIkNg=";
string EventValidation = "/wEWBAL0/ZalCAKG3rm9CgKPndvpBwKC3IeGDN9sVaGWqHIZJdkOjZJcmALtdHsu";
string submitButton = "登录";

submitButton = System.Web.HttpUtility.UrlEncode(submitButton);
viewState = System.Web.HttpUtility.UrlEncode(viewState);
eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
#endregion

//要提交表单的URI字符串
#region
string strUsername = this.textUsername.Text;
string strPassword = this.textPassword.Text;

//要提交的字符串数据。格式形如:user=uesr1&password=123
postString = "textUsername=" + strUsername + "&textPassword=" + strPassword;
postString += "&btnLogin=" + submitButton + "&__EVENTVALIDATION=" + eventValidation + "&__VIEWSTATE=" + viewState;

//将字符串转换成字节数组
postData = Encoding.ASCII.GetBytes(postString);
#endregion //要提交表单的URI字符串  //要提交表单的URI字符串
#endregion

//进行POST的准备工作了获取服务器返回信息,并GET Cookies
#region
//初始化HttpWebRequest
#region
//创建HttpWebRequest对象hwResquest
HttpWebRequest hwResquest = (HttpWebRequest)WebRequest.Create(new Uri(uriString));

//准备Web请求,初始化hwResquest
//写入开始后不能设置此项: hwResquest.ContentLength = postData.Length;
hwResquest.ContentLength = postData.Length;
hwResquest.Method = "POST";
hwResquest.CookieContainer = cookContainer;
hwResquest.ContentType = "application/x-www-form-urlencoded";
hwResquest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
#endregion

//保存或设置Cookie
#region
if (cookieheader.Equals(string.Empty))
{
cookieheader = hwResquest.CookieContainer.GetCookieHeader(new Uri(uriString));
}
if (!cookieheader.Equals(string.Empty) && cookieheader.Length != 0)
{
hwResquest.CookieContainer.SetCookies(new Uri(uriString), cookieheader);
cookContainer = hwResquest.CookieContainer;
}
#endregion

//向服务器发送POST数据的请求、处理应答、显示服务器应答结果
#region
try
{
//建立请求流、发送Post请求、关闭并释放流,当有Cookies时,获得Cookies
#region
//当在子类中重写时,返回用于将数据写入 Internet 资源的 Stream
System.IO.Stream requestStream = hwResquest.GetRequestStream();

//发送POST请求
requestStream.Write(postData, 0, postData.Length);

//关闭并释放流
requestStream.Close();
//当有Cookies时,获得Cookies
//hwResquest.GetResponse().Close();
//cookieCollection = hwResquest.CookieContainer.GetCookies(new Uri(uriString));
#endregion

//应答处理
#region
//初始化HttpWebResponse对象hwResponse为了获得服务器应答
HttpWebResponse hwResponse = (HttpWebResponse)hwResquest.GetResponse();

//判断是否成功
if (hwResponse.StatusCode == HttpStatusCode.OK)
Console.WriteLine("\nRequest succeeded and the requested information is in the response ,Description : {0}",
hwResponse.StatusDescription);
Uri myUri=new Uri (uriString );
if (myUri.Equals(hwResponse.ResponseUri))
Console.WriteLine("\nThe Request Uri was not redirected by the server");
else
Console.WriteLine("\nThe Request Uri was redirected to :{0}", hwResponse.ResponseUri);

Console.WriteLine("\nThe Uri that responded for the Request is   {0}", hwResquest.RequestUri);


// Release resources of response object.

//获得应答流,并转换为UTF8格式
System.IO.StreamReader responseStream = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
#endregion

//转换为字符串,显示服务器应答结果到re.Text
#region
stringResponse = responseStream.ReadToEnd();
responseStream.Close();

re.Text = stringResponse;
#endregion
}
catch (WebException we)
{
MessageBox.Show(we.Message);
}
#endregion
#endregion
MessageBox.Show("成功提交第1页");
#endregion
发的时候把代码放在[ code ] [ /code]这个标记中间就有格式了
菜牛 2013-02-17
  • 打赏
  • 举报
回复
你两个post是独立的,为什么不用WebClient?
bagdad123 2013-02-06
  • 打赏
  • 举报
回复
好帖子,学习了。
彖爻之辞 2011-11-04
  • 打赏
  • 举报
回复
//刚才贴的代码格式没有了,重新贴出来
//定义变量
#region
string postString = string.Empty; //Post字串
string stringResponse = string.Empty; //接收服务器应答字串
byte[] postData; //Post Data
ASCIIEncoding encoding = new ASCIIEncoding(); //代码转换时使用
string cookieheader = string.Empty; //Cookies Header
//CookieCollection cookieCollection; //创建CookieCollection对象cookieCollection
CookieContainer cookContainer = new CookieContainer(); //创建CookieContainer对象cookieContainer
#endregion

//第1页Login
#region
//初始化变量和POST串
#region

#region
string urlstring = "http://192.168.104.35:8001/";
string ViewState = "/wEPDwUKMTEyMTc3MTQwNmRkLjlNkqKUMAkS3auwl+zhtnVIkNg=";
string EventValidation = "/wEWBAL0/ZalCAKG3rm9CgKPndvpBwKC3IeGDN9sVaGWqHIZJdkOjZJcmALtdHsu";
string submitButton = "登录";

submitButton = System.Web.HttpUtility.UrlEncode(submitButton);
viewState = System.Web.HttpUtility.UrlEncode(viewState);
eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
#endregion

//要提交表单的URI字符串
#region
string strUsername = this.textUsername.Text;
string strPassword = this.textPassword.Text;

//要提交的字符串数据。格式形如:user=uesr1&password=123
postString = "textUsername=" + strUsername + "&textPassword=" + strPassword;
postString += "&btnLogin=" + submitButton + "&__EVENTVALIDATION=" + eventValidation + "&__VIEWSTATE=" + viewState;

//将字符串转换成字节数组
postData = Encoding.ASCII.GetBytes(postString);
#endregion //要提交表单的URI字符串 //要提交表单的URI字符串
#endregion

//进行POST的准备工作了获取服务器返回信息,并GET Cookies
#region
//初始化HttpWebRequest
#region
//创建HttpWebRequest对象hwResquest
HttpWebRequest hwResquest = (HttpWebRequest)WebRequest.Create(new Uri(uriString));

//准备Web请求,初始化hwResquest
//写入开始后不能设置此项: hwResquest.ContentLength = postData.Length;
hwResquest.ContentLength = postData.Length;
hwResquest.Method = "POST";
hwResquest.CookieContainer = cookContainer;
hwResquest.ContentType = "application/x-www-form-urlencoded";
hwResquest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
#endregion

//保存或设置Cookie
#region
if (cookieheader.Equals(string.Empty))
{
cookieheader = hwResquest.CookieContainer.GetCookieHeader(new Uri(uriString));
}
if (!cookieheader.Equals(string.Empty) && cookieheader.Length != 0)
{
hwResquest.CookieContainer.SetCookies(new Uri(uriString), cookieheader);
cookContainer = hwResquest.CookieContainer;
}
#endregion

//向服务器发送POST数据的请求、处理应答、显示服务器应答结果
#region
try
{
//建立请求流、发送Post请求、关闭并释放流,当有Cookies时,获得Cookies
#region
//当在子类中重写时,返回用于将数据写入 Internet 资源的 Stream
System.IO.Stream requestStream = hwResquest.GetRequestStream();

//发送POST请求
requestStream.Write(postData, 0, postData.Length);

//关闭并释放流
requestStream.Close();
//当有Cookies时,获得Cookies
//hwResquest.GetResponse().Close();
//cookieCollection = hwResquest.CookieContainer.GetCookies(new Uri(uriString));
#endregion

//应答处理
#region
//初始化HttpWebResponse对象hwResponse为了获得服务器应答
HttpWebResponse hwResponse = (HttpWebResponse)hwResquest.GetResponse();

//判断是否成功
if (hwResponse.StatusCode == HttpStatusCode.OK)
Console.WriteLine("\nRequest succeeded and the requested information is in the response ,Description : {0}",
hwResponse.StatusDescription);
Uri myUri=new Uri (uriString );
if (myUri.Equals(hwResponse.ResponseUri))
Console.WriteLine("\nThe Request Uri was not redirected by the server");
else
Console.WriteLine("\nThe Request Uri was redirected to :{0}", hwResponse.ResponseUri);

Console.WriteLine("\nThe Uri that responded for the Request is {0}", hwResquest.RequestUri);


// Release resources of response object.

//获得应答流,并转换为UTF8格式
System.IO.StreamReader responseStream = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
#endregion

//转换为字符串,显示服务器应答结果到re.Text
#region
stringResponse = responseStream.ReadToEnd();
responseStream.Close();

re.Text = stringResponse;
#endregion
}
catch (WebException we)
{
MessageBox.Show(we.Message);
}
#endregion
#endregion
MessageBox.Show("成功提交第1页");
#endregion
彖爻之辞 2011-11-04
  • 打赏
  • 举报
回复
//先提出一个提交第1页的C#程序

//定义变量
#region
string postString = string.Empty; //Post字串
string stringResponse = string.Empty; //接收服务器应答字串
byte[] postData; //Post Data
ASCIIEncoding encoding = new ASCIIEncoding(); //代码转换时使用
string cookieheader = string.Empty; //Cookies Header
//CookieCollection cookieCollection; //创建CookieCollection对象cookieCollection
CookieContainer cookContainer = new CookieContainer(); //创建CookieContainer对象cookieContainer
#endregion

//第1页Login
#region
//初始化变量和POST串
#region

#region
string urlstring = "http://192.168.104.35:8001/";
string ViewState = "/wEPDwUKMTEyMTc3MTQwNmRkLjlNkqKUMAkS3auwl+zhtnVIkNg=";
string EventValidation = "/wEWBAL0/ZalCAKG3rm9CgKPndvpBwKC3IeGDN9sVaGWqHIZJdkOjZJcmALtdHsu";
string submitButton = "登录";

submitButton = System.Web.HttpUtility.UrlEncode(submitButton);
viewState = System.Web.HttpUtility.UrlEncode(viewState);
eventValidation = System.Web.HttpUtility.UrlEncode(eventValidation);
#endregion

//要提交表单的URI字符串
#region
string strUsername = this.textUsername.Text;
string strPassword = this.textPassword.Text;

//要提交的字符串数据。格式形如:user=uesr1&password=123
postString = "textUsername=" + strUsername + "&textPassword=" + strPassword;
postString += "&btnLogin=" + submitButton + "&__EVENTVALIDATION=" + eventValidation + "&__VIEWSTATE=" + viewState;

//将字符串转换成字节数组
postData = Encoding.ASCII.GetBytes(postString);
#endregion //要提交表单的URI字符串 //要提交表单的URI字符串
#endregion

//进行POST的准备工作了获取服务器返回信息,并GET Cookies
#region
//初始化HttpWebRequest
#region
//创建HttpWebRequest对象hwResquest
HttpWebRequest hwResquest = (HttpWebRequest)WebRequest.Create(new Uri(uriString));

//准备Web请求,初始化hwResquest
//写入开始后不能设置此项: hwResquest.ContentLength = postData.Length;
hwResquest.ContentLength = postData.Length;
hwResquest.Method = "POST";
hwResquest.CookieContainer = cookContainer;
hwResquest.ContentType = "application/x-www-form-urlencoded";
hwResquest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
#endregion

//保存或设置Cookie
#region
if (cookieheader.Equals(string.Empty))
{
cookieheader = hwResquest.CookieContainer.GetCookieHeader(new Uri(uriString));
}
if (!cookieheader.Equals(string.Empty) && cookieheader.Length != 0)
{
hwResquest.CookieContainer.SetCookies(new Uri(uriString), cookieheader);
cookContainer = hwResquest.CookieContainer;
}
#endregion

//向服务器发送POST数据的请求、处理应答、显示服务器应答结果
#region
try
{
//建立请求流、发送Post请求、关闭并释放流,当有Cookies时,获得Cookies
#region
//当在子类中重写时,返回用于将数据写入 Internet 资源的 Stream
System.IO.Stream requestStream = hwResquest.GetRequestStream();

//发送POST请求
requestStream.Write(postData, 0, postData.Length);

//关闭并释放流
requestStream.Close();
//当有Cookies时,获得Cookies
//hwResquest.GetResponse().Close();
//cookieCollection = hwResquest.CookieContainer.GetCookies(new Uri(uriString));
#endregion

//应答处理
#region
//初始化HttpWebResponse对象hwResponse为了获得服务器应答
HttpWebResponse hwResponse = (HttpWebResponse)hwResquest.GetResponse();

//判断是否成功
if (hwResponse.StatusCode == HttpStatusCode.OK)
Console.WriteLine("\nRequest succeeded and the requested information is in the response ,Description : {0}",
hwResponse.StatusDescription);
Uri myUri=new Uri (uriString );
if (myUri.Equals(hwResponse.ResponseUri))
Console.WriteLine("\nThe Request Uri was not redirected by the server");
else
Console.WriteLine("\nThe Request Uri was redirected to :{0}", hwResponse.ResponseUri);

Console.WriteLine("\nThe Uri that responded for the Request is {0}", hwResquest.RequestUri);


// Release resources of response object.

//获得应答流,并转换为UTF8格式
System.IO.StreamReader responseStream = new StreamReader(hwResponse.GetResponseStream(), Encoding.UTF8);
#endregion

//转换为字符串,显示服务器应答结果到re.Text
#region
stringResponse = responseStream.ReadToEnd();
responseStream.Close();

re.Text = stringResponse;
#endregion
}
catch (WebException we)
{
MessageBox.Show(we.Message);
}
#endregion
#endregion
MessageBox.Show("成功提交第1页");
#endregion

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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