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)