Two methods for the Get string type of HTTP and post custom classes--Test succeeded

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.