码迷,mamicode.com
首页 > Windows程序 > 详细

C#(Winform) Http 发送数据

时间:2016-02-19 15:50:13      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:

Get方式

 private string HttpGet(string url, string postData) {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + (postData == "" ? "" : "?") + postData);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

Post方式

private string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            byte[] data = Encoding.GetEncoding("utf-8").GetBytes(postDataStr);

            request.ContentLength = data.Length;

            Stream myRequestStream = request.GetRequestStream();

            myRequestStream.Write(data,0,data.Length);
            myRequestStream.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

 

C#(Winform) Http 发送数据

标签:

原文地址:http://www.cnblogs.com/Jett/p/5201186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!