C# http返回Cookie以后使用

 1 public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cookie)
 2         {
 3             //向服务端请求
 4             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
 5             myRequest.ContentType = "application/x-www-form-urlencoded";
 6             myRequest.CookieContainer = new CookieContainer();
 7             //将请求的结果发送给客户端(界面、应用)
 8             HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
 9             cookie.Add(myResponse.Cookies);
10             StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding);
11             return reader.ReadToEnd();
12         }
13 
14         public static string GetHtml(string requestUrlString, Encoding encoding, CookieContainer cookie)
15         {
16             string ua = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
17             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
18             myRequest.ContentType = "application/x-www-form-urlencoded";
19             myRequest.UserAgent = ua;
20             myRequest.CookieContainer = cookie;
21             HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
22             StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding);
23             return reader.ReadToEnd();
24         }
25 
26         public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie)
27         {
28             ASCIIEncoding encoding = new ASCIIEncoding();
29             byte[] data = encoding.GetBytes(postData);
30             //向服务端请求
31             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
32             myRequest.Method = "POST";
33             myRequest.ContentType = "application/x-www-form-urlencoded";
34             myRequest.ContentLength = data.Length;
35             myRequest.CookieContainer = new CookieContainer();
36             Stream newStream = myRequest.GetRequestStream();
37             newStream.Write(data, 0, data.Length);
38             newStream.Close();
39             //将请求的结果发送给客户端(界面、应用)
40             HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
41             cookie.Add(myResponse.Cookies);
42             StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
43             return reader.ReadToEnd();
44         }
45 
46         public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie)
47         {
48             ASCIIEncoding encoding = new ASCIIEncoding();
49             byte[] data = encoding.GetBytes(postData);
50             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString);
51             myRequest.Method = "POST";
52             myRequest.ContentType = "application/x-www-form-urlencoded";
53             myRequest.ContentLength = data.Length;
54             myRequest.CookieContainer = cookie;
55             Stream newStream = myRequest.GetRequestStream();
56             newStream.Write(data, 0, data.Length);
57             newStream.Close();
58             HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
59             StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
60             return reader.ReadToEnd();
61         }

 

posted @ 2022-04-21 10:43  學海無邊  阅读(195)  评论(0编辑  收藏  举报