WebRequest的get及post提交

 static string get_html(string url)
        {
            var request = WebRequest.Create(url);
            var response = request.GetResponse();
            var html = "";
            using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                html = sr.ReadToEnd();
            }
            return html;
        }
   public static string PostData()
        {
            var html = "";
            var account = "xxxx"; var sign = md5(pwd);
            string postdata = string.Format("Account={0}&Sign={1}",  account,  sign);
            //****************
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:4500/xxxx.ashx");
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 20000;
            byte[] btBodys = Encoding.UTF8.GetBytes(postdata);
            httpWebRequest.ContentLength = btBodys.Length;
            httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
            //****************
            var response = httpWebRequest.GetResponse();
            using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                html = sr.ReadToEnd();
            }
            return html;

        }

 

posted @ 2017-06-19 19:19  幸福安康  阅读(228)  评论(0编辑  收藏  举报