C# Post Json数据到对方url

1.

        /// <summary>
        /// 调用对方Url,Post上传数据
        /// </summary>
        /// <param name="postData"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string PostData(string postData)
        {
            //string url = ConfigurationManager.AppSettings["YuTaiUrl"];
            string url = "对方url地址";
            string respStr = string.Empty;
            using (WebClient client = new WebClient())
            {
                var data = Encoding.UTF8.GetBytes((string)postData);
                var response = client.UploadData(url, data);

                respStr = Encoding.UTF8.GetString(response);
            }
            return respStr;
        }




 2.

 public static string Post(string jsonParas)
        {
            //string strURL = Url + "/" + methodName;
            string strURL = "对方url地址";

            //创建一个HTTP请求  
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            //Post请求方式  
            request.Method = "POST";
            //内容类型
            request.ContentType = "application/x-www-form-urlencoded";
            //request.ContentType = "text/html";


            //设置参数,并进行URL编码  
            //虽然我们需要传递给服务器端的实际参数是JsonParas(格式:[{"UserID":"0206001","UserName":"ceshi"}]),
            //但是需要将该字符串参数构造成键值对的形式(注:"paramaters=[{"UserID":"0206001","UserName":"ceshi"}]"),
            //其中键paramaters为WebService接口函数的参数名,值为经过序列化的Json数据字符串
            //最后将字符串参数进行Url编码
            string paraUrlCoded = System.Web.HttpUtility.UrlEncode("paramaters");
            paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(jsonParas);

            byte[] payload;
            //将Json字符串转化为字节  
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            //设置请求的ContentLength   
            request.ContentLength = payload.Length;
            //发送请求,获得请求流  

            Stream writer;
            try
            {
                writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
            }
            catch (Exception)
            {
                writer = null;
                Console.Write("连接服务器失败!");
            }
            //将请求参数写入流
            writer.Write(payload, 0, payload.Length);
            writer.Close();//关闭请求流

            String strValue = "";//strValue为http响应所返回的字符流
            HttpWebResponse response;
            try
            {
                //获得响应流
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }

            Stream s = response.GetResponseStream();

            //服务器端返回的是一个XML格式的字符串,XML的Content才是我们所需要的Json数据
            XmlTextReader Reader = new XmlTextReader(s);
            Reader.MoveToContent();
            strValue = Reader.ReadInnerXml();//取出Content中的Json数据
            Reader.Close();
            s.Close();

            return strValue;//返回Json数据
        }

 3.

        /// <summary>
        /// POST请求接口调用
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static string PostMoths(string url, string param)
        {
            string strURL = url;
            System.Net.HttpWebRequest request;
            request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            string paraUrlCoded = param;
            byte[] payload;
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            request.ContentLength = payload.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(payload, 0, payload.Length);
            writer.Close();
            System.Net.HttpWebResponse response;
            response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream s;
            s = response.GetResponseStream();
            string StrDate = "";
            string strValue = "";
            StreamReader Reader = new StreamReader(s, Encoding.UTF8);
            while ((StrDate = Reader.ReadLine()) != null)
            {
                strValue += StrDate + "
";
            }
            return strValue;
        }
原文地址:https://www.cnblogs.com/Violety/p/9558993.html