C#发送post Get 请求

// post请求   application/json

 

 public string SendPost(string url, string dtStr)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(dtStr);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.ContentLength = requestBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
            string result = sr.ReadToEnd();
            sr.Close();
            res.Close();
            return result;
        }

//

public string SendPost2(string url, Dictionary obj)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            string dataStr = JsonConvert.SerializeObject(obj);
            byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(dataStr);
            req.Method = "POST";
            req.ContentType = "application/json";
            req.ContentLength = requestBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
            string result = sr.ReadToEnd();
            sr.Close();
            res.Close();
            return result;
        }

 

// 2 application/x-www-form-urlencoded

 public string SendPost3(string url, string dtStr)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            byte[] requestBytes = System.Text.Encoding.UTF8.GetBytes(dtStr);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = requestBytes.Length;
            Stream requestStream = req.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
            string result = sr.ReadToEnd();
            sr.Close();
            res.Close();
            return result;
        }

 

// get请求

 public string SendGet(string url) {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.ContentType = "application/json";

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
            string result = sr.ReadToEnd();
            sr.Close();
            res.Close();
            return result;
        }

 

你可能感兴趣的:(C#发送post Get 请求)