1. Arguments of type string, get method
<summary>//////</summary>//<param name= "MethodName" > method name </para m>//<param name= "pars" > Parameter list </param>//<param name= "_type" > Return value 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. Custom type with POST method
<summary>////Perform service methods with complex type parameters///</summary>//<param name= "MethodName" > Type name & lt;/param>//<param name= "pars" > Parameter list </param>//<param name= "_type" > type of Return value </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 = ""; Serialization 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"); Incoming parameter Stream requeststream = Request. GetRequestStream (); requestStream.Write (buffs, 0, buffs. Length); Requeststream.close (); Gets the return information 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; }
Two methods for the Get string type of HTTP and post custom classes--Test succeeded