首页 > 代码库 > http的get字符串类型和post自定义类的两种方法--测试成功

http的get字符串类型和post自定义类的两种方法--测试成功

1. string类型的参数,get方法

/// <summary>
        /// 执行服务的方法
        /// </summary>
        /// <param name="MethodName">方法名称</param>
        /// <param name="pars">参数列表</param>
        /// <param name="_type">返回值类型</param>
        /// <returns></returns>
        public object ExeJsonMethod(string MethodName, List<object> pars, Type _type)
        {
            string strResult = string.Empty;
            string url = DataLib.JsoinServiceURL + MethodName;
            foreach (object _par in pars)
            {
                url += (@"/" + _par.ToString());
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = 30000;
            request.Headers.Set("Pragma", "no-cache");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = Encoding.GetEncoding("UTF-8");
            StreamReader streamReader = new StreamReader(streamReceive, encoding);
            strResult = streamReader.ReadToEnd();
            var serialize = new JavaScriptSerializer();
            var obj = serialize.Deserialize(strResult, _type);
            return obj;
        }

2.自定义的类型用post方法

/// <summary>
        ///  执行含有复杂类型参数的服务方法
        /// </summary>
        /// <param name="MethodName">类型名</param>
        /// <param name="pars">参数列表</param>
        /// <param name="_type">返回值的类型</param>
        /// <returns></returns>
        public object ExeJsonMethodPostPar(string MethodName, object pars, Type _type)
        {
            string baseURL = @"http://localhost:9001/MobileHISRESTService.svc/VerifyCertifications";
            DCUserCertification de = new DCUserCertification() { CertificationContent = @"das", CertificationPassword = "dsa" };
            DataContractJsonSerializer json = new DataContractJsonSerializer(pars.GetType());
            string szJson = "";
            //序列化           
            using (MemoryStream stream = new MemoryStream())
            {
                json.WriteObject(stream, pars);
                szJson = Encoding.UTF8.GetString(stream.ToArray());
            }
            byte[] buffs = Encoding.Default.GetBytes(szJson);
            string strResult = "";
            WebRequest request = WebRequest.Create(baseURL);
            request.Method = @"POST";
            request.ContentType = @"application/json";
            request.ContentLength = buffs.Length;
            request.Timeout = 5000;
            request.Headers.Set("Pragma", "no-cache");
            ////传入参数
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(buffs, 0, buffs.Length);
            requestStream.Close();
            //获取返回信息
            WebResponse response = request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(streamReceive);
            strResult = streamReader.ReadToEnd();
            var serialize = new JavaScriptSerializer();
            var obj = serialize.Deserialize(strResult, _type);
            return obj;
        }




http的get字符串类型和post自定义类的两种方法--测试成功