110,534
社区成员
发帖
与我相关
我的任务
分享
void Print_Cookie(HttpWebResponse response)
{
foreach (Cookie cook in response.Cookies)
{
Console.WriteLine("Cookie:");
Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
Console.WriteLine("Domain: {0}", cook.Domain);
Console.WriteLine("Path: {0}", cook.Path);
Console.WriteLine("Port: {0}", cook.Port);
Console.WriteLine("Secure: {0}", cook.Secure);
Console.WriteLine("When issued: {0}", cook.TimeStamp);
Console.WriteLine("Expires: {0} (expired? {1})",
cook.Expires, cook.Expired);
Console.WriteLine("Don't save: {0}", cook.Discard);
Console.WriteLine("Comment: {0}", cook.Comment);
Console.WriteLine("Uri for comments: {0}", cook.CommentUri);
Console.WriteLine("Version: RFC {0}", cook.Version == 1 ? "2109" : "2965");
// Show the string representation of the cookie.
Console.WriteLine("String: {0}", cook.ToString());
}
}
void Post(string url, string indata, string strCookie)
{
string outdata = "";
CookieContainer myCookieContainer = new CookieContainer();
//新建一个CookieContainer来存放Cookie集合
//打开页面
HttpWebRequest myHttpWebRequest1 = (HttpWebRequest)WebRequest.Create("http://www.7daysinn.cn/login.php");
//新建一个HttpWebRequest
myHttpWebRequest1.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest1.ContentLength = indata.Length;
//myHttpWebRequest1.Method = "";
myHttpWebRequest1.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)";
myHttpWebRequest1.CookieContainer = myCookieContainer;
HttpWebResponse response1 = myHttpWebRequest1.GetResponse() as HttpWebResponse;
response1.Cookies = myHttpWebRequest1.CookieContainer.GetCookies(myHttpWebRequest1.RequestUri);
Print_Cookie(response1);
//打开页面
string url_sc = "http://www.7daysinn.cn/safecode.php";
HttpWebRequest myHttpWebRequest2 = (HttpWebRequest)WebRequest.Create(url_sc);
//新建一个HttpWebRequest
myHttpWebRequest2.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest2.ContentLength = indata.Length;
myHttpWebRequest2.Method = "GET";
myHttpWebRequest2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)";
myHttpWebRequest2.CookieContainer = new CookieContainer();
myHttpWebRequest2.CookieContainer.Add(response1.Cookies);
HttpWebResponse response2 = myHttpWebRequest2.GetResponse() as HttpWebResponse;
response2.Cookies = myHttpWebRequest2.CookieContainer.GetCookies(myHttpWebRequest2.RequestUri);
Print_Cookie(response2);
System.Drawing.Image simg = System.Drawing.Image.FromStream(response2.GetResponseStream());
simg.Save("temp1.bmp");
Bitmap newimg = new Bitmap(simg);
pictureBox1.Image = simg;
//以下使用GetVcodeFromBuffer接口
StringBuilder Result = new StringBuilder('\0', 256);
string ImgPath = System.Environment.CurrentDirectory + "\\temp1.bmp";
FileStream fsMyfile = File.OpenRead(ImgPath);
int FileLen = (int)fsMyfile.Length;
byte[] Buffer = new byte[FileLen];
fsMyfile.Read(Buffer, 0, FileLen);
fsMyfile.Close();
if (GetVcodeFromBuffer(index, Buffer, FileLen, Result))
indata += Result.ToString();
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
//新建一个HttpWebRequest
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Q312461; .NET CLR 1.0.3705)";
myHttpWebRequest.CookieContainer = new CookieContainer();
myHttpWebRequest.CookieContainer.Add(response2.Cookies);
//设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(indata, 0, indata.Length);
//把数据写入HttpWebRequest的Request流
myStreamWriter.Close();
myRequestStream.Close();
//关闭打开对象
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
//新建一个HttpWebResponse
myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
//获取一个包含url的Cookie集合的CookieCollection
Print_Cookie(myHttpWebResponse);
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
outdata = myStreamReader.ReadToEnd();
//把数据从HttpWebResponse的Response流中读出
myStreamReader.Close();
myResponseStream.Close();
Console.WriteLine(outdata);