C#实现请求服务器,类似于asp下的getHTTPPage(url)功能

编写一个类,需要添加一些引用

using System.Net;
using System.Text;
using System.IO;

代码如下

        public static CookieContainer Cook = new CookieContainer();
        public static string CreatRequestGetResponse(string URL, string PostData)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
            myRequest.CookieContainer = Cook;
            myRequest.KeepAlive = true;
            myRequest.Method = "POST";
            string postdata = PostData;
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            byte[] postarr = encode.GetBytes(postdata);
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = postarr.Length;
            Stream outStream = myRequest.GetRequestStream();
            outStream.Write(postarr, 0, postarr.Length);
            outStream.Close();
            WebResponse myResponse = myRequest.GetResponse();
            StreamReader SReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
            return SReader.ReadToEnd();
        }
原文地址:https://www.cnblogs.com/Raywang80s/p/2759134.html