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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39885282/article/details/80497253

一、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;
            }

        }
展开阅读全文

没有更多推荐了,返回首页