17,748
社区成员




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吗//先提出一个提交第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]这个标记中间就有格式了