温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

post提交获得html页面源码的案例

发布时间:2020-10-26 09:29:38 来源:亿速云 阅读:314 作者:小新 栏目:web开发

小编给大家分享一下post提交获得html页面源码的案例,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

/// <summary>        /// 获得页面的html源码  主要用于后台生成静态文件时获得源码        /// </summary>        /// <param name="url"></param>        /// <returns></returns>        public static string GetPageHTML(string url)
        {
            string httpString = string.Empty;
            WebRequest request = WebRequest.Create(url);
            request.Timeout = 200000;            
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusDescription.ToLower().Equals("ok"))
                {
                    using (StreamReader writer = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312")))
                    {
                        httpString = writer.ReadToEnd();
                    }
                }
            }
            return httpString;
        }
 
 
        /// <summary>        /// 获得页面的html源码  主要用于后台生成静态文件时获得源码UTF-8        /// </summary>        /// <param name="url"></param>        /// <returns></returns>        public static string GetPageHTMLUTF8(string url)
        {
            string httpString = string.Empty;
            WebRequest request = WebRequest.Create(url);
            request.Timeout = 200000;            
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusDescription.ToLower().Equals("ok"))
                {
                    using (StreamReader writer = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
                    {
                        httpString = writer.ReadToEnd();
                    }
                }
            }
            return httpString;
        }
 
 
        /// <summary>        /// post提交JSON数据。支持.net4.0及以下的版本        /// </summary>        /// <param name="url"></param>        /// <param name="json"></param>        /// <returns></returns>        public static string GetHtmlByJson(string url, string json = "")
        {
            var result = string.Empty;
 
            try            {
                var request = WebRequest.Create(url) as HttpWebRequest;
                request.ContentType = "text/json";
                request.Method = "post";
                //request.CookieContainer = _cookie;
                 using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
 
                    var response = (HttpWebResponse)request.GetResponse();
 
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    }
                }
            }
            catch (UriFormatException uex)
            {
                // 出错处理            }
 
            return result;
        }
 
        /// <summary>         /// 利用WebClient 远程POST数据并返回数据         /// </summary>         /// <param name="strUrl">远程URL地址</param>         /// <param name="strParams">参数,要提交的JSON字符串</param>         /// <param name="RespEncode">POST数据的编码</param>         /// <param name="ReqEncode">获取数据的编码</param>         /// <returns></returns>         public static string PostData(string strUrl, string strParams, Encoding RespEncode, Encoding ReqEncode)
        {
            /**             * 本函数只支持.net4.5以上的框架            HttpClient httpclient = new HttpClient();            try            {                //打开页面                 httpclient.Credentials = CredentialCache.DefaultCredentials;                //从指定的URI下载资源                 byte[] responseData = httpclient.DownloadData(strUrl);                string srcString = RespEncode.GetString(responseData);
                 httpclient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");                string postString = strParams;                // 将字符串转换成字节数组                 byte[] postData = Encoding.ASCII.GetBytes(postString);                // 上传数据,返回页面的字节数组                 responseData = httpclient.UploadData(strUrl, "POST", postData);                srcString = ReqEncode.GetString(responseData);
                 return srcString;            }            catch (Exception ex)            {                //记录异常日志                 //释放资源                 httpclient.Dispose();                return string.Empty;            }            */            return "";
        }
 
 
        /// <summary>        /// 执行POST提交范例        /// </summary>        /// <param name="url"></param>        /// <param name="postdata">"LoginName=365admin&Password=fob123"</param>        /// <returns></returns>        public static string PostPageHTMLUTF8(string url, string postdata)
        {
            WebClient client = new WebClient();
            System.Collections.Specialized.NameValueCollection list = new System.Collections.Specialized.NameValueCollection();            
            list.Add("opencheckindatatype", "3");
            list.Add("starttime", "1492617600");
            list.Add("endtime", "1492790400");
            // "useridlist": ["james","paul"]                          list.Add("useridlist", "['TuHuaXing']");            
            byte[] j = client.UploadValues(url, list);
            //var dec = BitConverter.ToInt64(j,0);            //string jS = BitConverter.ToString(j);            //jS = Convert.ToString(jS,10);            return System.Text.Encoding.Default.GetString(j);
 
            //目标页面获取值   request.form["id"]
             /**            WebRequest request7 = WebRequest.Create(url);            request7.Method = "POST";
             //post传参数                        byte[] bytes = Encoding.ASCII.GetBytes(postdata);            request7.ContentType = "application/x-www-form-urlencoded";            request7.ContentLength = postdata.Length;            request7.S            Stream sendStream = request7.GetRequestStream();            sendStream.Write(bytes, 0, bytes.Length);            sendStream.Close();
             //得到返回值              WebResponse response7 = request7.GetResponse();            string OrderQuantity = new StreamReader(response7.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();            return OrderQuantity;            //转化成json对象处理            //List<GetOrderQuantity> getOrderQuantity = sr.Deserialize<List<GetOrderQuantity>>(OrderQuantity);            **/        }

看完了这篇文章,相信你对post提交获得html页面源码的案例有了一定的了解,想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI