MVC POST待参测试

目的:测试C#Post能带什么参和传什么参数类型。

结果:能带string,[FromBody]JObject 【表单json】、int 、多个string,int。out int 参数类型测试失败。带out的参数暂时还没解决,给朋友留言解决方法。

客户端:

 //,传一个参数的Post接口
             string sds = "{ \"Name\": \"Test2\" }";
             string dss = PostHttpUrl("http://localhost:4393/api/values?key=15265", sds);

             //,传两个参数的Post接口
             string ds = "{ \"Name\": \"Test3\" }";
             string sdsf = PostHttpUrl("http://localhost:4393/api/values?key=15265&autograph=dsds", ds);

             //,传两个参数的Post接口
             string hbg = "{ \"Name\": \"Test3\" }";
             string tbbt = PostHttpUrl("http://localhost:4393/api/values?key=&autograph=dsds", hbg);


             //一个带int的参
             string thbbt = PostHttpUrl2("http://localhost:4393/api/values?sdsd=2");

/****************************************************/
             //一个带out int的参测试失败
            // string thdsbbt = PostHttpUrl2("http://localhost:4393/api/values?sdsd=2");

 /// <summary>
        /// .net 后台 post http地址请求
        /// </summary>
        /// <param name="uri">请求地址</param>
        /// <param name="postData">请求数据</param>
        /// type=application/x-www-form-urlencoded  
        /// type=application/json;charset=UTF-8
        /// <returns></returns>
        private string PostHttpUrl(string uri, string postData)
        {
            try
            {
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData); //字符串转换成ASCII码,有利于流的传输
                HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(uri); //发送地址
                objWebRequest.Method = "POST";//提交方式
                objWebRequest.ContentType = "application/json;charset=UTF-8";//获取的值的类型,Json
                objWebRequest.ContentLength = byteArray.Length; //需要发送的内容的大小
                using (Stream newStream = objWebRequest.GetRequestStream())
                {
                    newStream.Write(byteArray, 0, byteArray.Length); //写入参数
                    newStream.Close();
                }
                HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();//获取响应
                StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);//重新把获取数据的流转换成ASCII码
                return sr.ReadToEnd(); // 返回的数据,从头把流转换成字符
            }
            catch (Exception ex)
            {
                return "网络错误:" + ex.Message.ToString();
            }
        }

服务端:


        // POST api/values
        public HttpResponseMessage Post([FromBody]JObject value)
        {
            string Js = value.ToString();
            string json = "{\"Age\":85,\"Name\":\"不知道\"\"}";
            return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
        }


        // POST api/values 两个参数的Post
        public HttpResponseMessage Post([FromBody]JObject value,string Key)
        {
            string Js = value.ToString();
            string json = "{\"Age\":85,\"Name\":\"不知道\"\"}";
            return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
        }


        // POST api/values 两个参数的Post
        public HttpResponseMessage Post([FromBody]JObject value, string Key, string autograph)
        {
            string Js = value.ToString();
            string json = "{\"Age\":85,\"Name\":\"不知道\"\"}";
            return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
        }

        // POST api/values int参数
        public HttpResponseMessage Post(int sdsd)
        {
            
            string json = "{\"Age\":85,\"Name\":\"不知道\"\"}";
            return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
        }


        // POST api/values 两个参数+一个不确定的参数的Post
        public HttpResponseMessage Post(out int sdsd)
        {
            sdsd = 5;
            string json = "{\"Age\":85,\"Name\":\"不知道\"\"}";
            return new HttpResponseMessage { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };
        }

没有上传案例,代码做参考。

猜你喜欢

转载自blog.csdn.net/weixin_42401291/article/details/83746358
MVC
今日推荐