static void Main(string[] args)
{
CookieContainer myCookieContainer=new CookieContainer();
Console.WriteLine(PostData("http://localhost/csdn/step1.asp","step1=step1",myCookieContainer));
Console.WriteLine(PostData("http://localhost/csdn/step2.asp","",myCookieContainer));
Console.WriteLine(PostData("http://localhost/csdn/step3.asp","",myCookieContainer));
}
{
CookieContainer myCookieContainer=new CookieContainer();
Console.WriteLine(PostData("http://localhost/csdn/step1.asp","step1=step1",myCookieContainer));
Console.WriteLine(PostData("http://localhost/csdn/step2.asp","",myCookieContainer));
Console.WriteLine(PostData("http://localhost/csdn/step3.asp","",myCookieContainer));
}
写一个方法封装一下这个post的操作,第一个参数是地址,第二个参数是post的信息,第三个参数是CookieContainer,request的时候使用这个CookieContainer,在response的时候把CookieContainer赋值,因为本来CookieContainer就是引用类型的,所以可以不加ref或者out操作符。
public static string PostData(string url,string indata,CookieContainer myCookieContainer)
{
string outdata="";
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength=indata.Length;
myHttpWebRequest.Method="POST";
myHttpWebRequest.CookieContainer=myCookieContainer;
Stream myRequestStream=myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
myStreamWriter.Close();
myRequestStream.Close();
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
Stream myResponseStream=myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312"));
outdata=myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return outdata;
}
{
string outdata="";
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.ContentType="application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength=indata.Length;
myHttpWebRequest.Method="POST";
myHttpWebRequest.CookieContainer=myCookieContainer;
Stream myRequestStream=myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata);
myStreamWriter.Close();
myRequestStream.Close();
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
Stream myResponseStream=myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312"));
outdata=myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return outdata;
}
三个asp页面如下:
step1.asp
<%
if request.ServerVariables("REQUEST_METHOD")="POST" then
if request.Form("step1")="step1" then
session("step")="1"
response.Write("<script>alert('step1->ok');location.href='step2.asp';</script>")
else
response.Write("<script>alert('step1->not ok');</script>")
end if
end if
%>
<form method="post">
<input type="text" name="step1" value="step1">
<input type="submit" value="post">
</form>
if request.ServerVariables("REQUEST_METHOD")="POST" then
if request.Form("step1")="step1" then
session("step")="1"
response.Write("<script>alert('step1->ok');location.href='step2.asp';</script>")
else
response.Write("<script>alert('step1->not ok');</script>")
end if
end if
%>
<form method="post">
<input type="text" name="step1" value="step1">
<input type="submit" value="post">
</form>
step2.asp
<%
if session("step")="1" then
session("step")="2"
response.Write("<script>alert('step2->ok');location.href='step3.asp';</script>")
else
response.Write("<script>alert('step2->not ok');</script>")
end if
%>
if session("step")="1" then
session("step")="2"
response.Write("<script>alert('step2->ok');location.href='step3.asp';</script>")
else
response.Write("<script>alert('step2->not ok');</script>")
end if
%>
step3.asp
<%
if session("step")="2" then
response.Write("<script>alert('step3->ok');</script>")
else
response.Write("<script>alert('step3->not ok');</script>")
end if
%>
if session("step")="2" then
response.Write("<script>alert('step3->ok');</script>")
else
response.Write("<script>alert('step3->not ok');</script>")
end if
%>