c# http网页 GET POST

 1 #region http网页 GET POST
 2         /// <summary>
 3         /// GET提交数据获取网页源码
 4         /// </summary>
 5         /// <param name="Url">网址</param>
 6         /// <param name="postDataStr">GET数据</param>
 7         /// <param name="cookieStr">cookie记录</param>
 8         /// <returns></returns>
 9         public static string HttpGet(string Url, string postDataStr, string cookieStr)
10         {
11             try
12             {
13                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
14                 request.Method = "GET";
15                 request.ContentType = "text/html;charset=UTF-8";
16                 request.Headers.Add("Cookie", cookieStr);
17                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
18                 Stream myResponseStream = response.GetResponseStream();
19                 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
20                 string retString = myStreamReader.ReadToEnd();
21                 myStreamReader.Close();
22                 myResponseStream.Close();
23                 return retString;
24             }
25             catch (Exception ex)
26             {
27                 return ex.Message;
28             }
29         }
30         /// <summary>
31         /// POST提交数据获取网页源码
32         /// </summary>
33         /// <param name="url">网址</param>
34         /// <param name="postData">GET数据</param>
35         /// <param name="cookieStr">cookie记录</param>
36         /// <returns></returns>
37         public static string HttpPost(string url, string postData, string cookieStr)
38         {
39             try
40             {
41                 //data
42                 //string postData = string.Format("userid={0}&password={1}", "guest", "123456");
43                 byte[] data = Encoding.UTF8.GetBytes(postData);
44                 // Prepare web request...
45                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
46                 request.Method = "POST";
47                 request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
48                 request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
49                 //request.Host = "www.xxx.com";
50                 request.Headers.Add("Cookie", cookieStr);
51                 request.ContentLength = data.Length;
52                 Stream newStream = request.GetRequestStream();
53                 // Send the data.
54                 newStream.Write(data, 0, data.Length);
55                 newStream.Close();
56                 // Get response
57                 HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
58                 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
59                 string content = reader.ReadToEnd();
60                 return content;
61             }
62             catch (Exception)
63             {
64                 //throw;
65             }
66             return "";
67         }
68         #endregion

 

posted @ 2020-08-20 10:51  學海無邊  阅读(216)  评论(0编辑  收藏  举报