C#实现post和get方式的请求

一、post:
需要引用:
using System.Net;

public static String postOrderDate(String data, String url)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = "application/x-www-form-urlencoded;charset=\"utf-8\"";
                request.Method = "post";
                request.Timeout = 10000;
                byte[] dataStr = Encoding.UTF8.GetBytes(data);
                request.ContentLength = dataStr.Length;

                Stream requestream = request.GetRequestStream();
                requestream.Write(dataStr, 0, dataStr.Length);
                requestream.Close();


                using (HttpWebResponse rs = (HttpWebResponse)request.GetResponse())
                {
                    StreamReader sr = new StreamReader(rs.GetResponseStream(), Encoding.UTF8);
                    return sr.ReadToEnd();
                }

            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

你可能感兴趣的:(C#)