C #-based interface Automation test (i)

Source: Internet
Author: User

It's really about finding a place and making a note of the key code ...

String Access API interface, access method is post:

 string url = URL;            string requestparam = Param;            string headername = Headername;            String Header = header;            String html = "";            try {WebRequest wbreq = webrequest.create (URL); } catch (WebException WebEx) {Console.WriteLine ("Unreachable uri:" + "\ r \ n" + Webex.tostrin            g ());            } byte[] ByteArray = Encoding.UTF8.GetBytes (Requestparam); {//post Access interface HttpWebRequest requestinterfacerequsetbystring = (HttpWebRequest) HttpWebRequest .                Create (New Uri (URL));                Requestinterfacerequsetbystring.keepalive = false;                Requestinterfacerequsetbystring.protocolversion = httpversion.version11;                Requestinterfacerequsetbystring.method = "POST";                Requestinterfacerequsetbystring.contenttype = "application/x-www-form-urlencoded"; Requestinterfacerequsetbystring.timeout = -1;//timeout set to infinite requestinterfacerequsetbystring.useragent = "mozilla/5.0 (Windows NT 6.1; WOW64;                rv:42.0) gecko/20100101 firefox/42.0 ";                Encoding Encoding = encoding.getencoding ("Utf-8");                REQUESTINTERFACEREQUSETBYSTRING.HEADERS.ADD (Headername, Header);                    try {Stream requeststream = Requestinterfacerequsetbystring.getrequeststream (); Requeststream.                    Write (ByteArray, 0, bytearray.length); Requeststream.                    Close (); try {httpwebresponse response = (HttpWebResponse) Requestinterfacerequsetbystrin                        G.getresponse (); Stream Responsestream = Response.                        GetResponseStream ();                        StreamReader sr = new StreamReader (responsestream); html = Sr.   ReadToEnd (); Read from the beginning to the end and put it into the string HTML responsestream.       Close ();                 Response.                    Close (); } catch (Exception ex) {html = ex.                    Message; }} catch (Exception ex) {html = ex.                Message;        }} return HTML; }

JSON to Dictionary type:

  Public Staticdictionary<string,Object> Convertdictionary (stringstr) {JavaScriptSerializer JSS=NewJavaScriptSerializer (); //string jsstr = convertjsonstring (str);            Try            {                returnJss. deserialize<dictionary<string,Object>>(str); }            Catch(Exception ex) {Throw NewException (ex.            Message); }        }

Dictionary type Access API interface: basically similar to string type access interface, but need to add dictionary related things, access method is post

dictionary<string,Object> requestdic =converttodictionary.convertdictionary (Requestparam); StringBuilder Buffer=NewStringBuilder (); inti =0; foreach(stringKeyinchRequestdic. Keys) {if(I >0) {buffer. AppendFormat ("&{0}={1}", Key, Requestdic[key]); }                Else{buffer. AppendFormat ("{0}={1}", Key, Requestdic[key]); } I++; }            byte[] ByteArray = Encoding.UTF8.GetBytes (buffer. ToString ());

Format the JSON string:

 Public Static stringConvertjsonstring (stringstr) {            //formatting a JSON stringJsonserializer serializer =NewJsonserializer (); TextReader TR=NewStringReader (str); JsonTextReader JTR=NewJsonTextReader (TR); Objectobj =Serializer.                Deserialize (JTR); if(obj! =NULL) {StringWriter textWriter=NewStringWriter (); JsonTextWriter Jsonwriter=NewJsonTextWriter (textWriter) {formatting=formatting.indented, Indentation=4, IndentChar=' '                    }; Serializer.                    Serialize (Jsonwriter, obj); returntextwriter.tostring (); }                Else                {                    returnstr; }            }        }

C #-based interface Automation test (i)

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.