1.Post, GET request
Public Static stringHttpPost (stringUrlstringparam) {HttpWebRequest Request=(HttpWebRequest) httpwebrequest.create (URL); Request. Method="POST"; Request. ContentType="application/x-www-form-urlencoded"; Request. Accept="*/*"; Request. Timeout=15000; Request. AllowAutoRedirect=false; StreamWriter Requeststream=NULL; WebResponse response=NULL; stringResponsestr =NULL; Try{requeststream=NewStreamWriter (Request. GetRequestStream ()); requestStream.Write (param); Requeststream.close (); Response=request. GetResponse (); if(Response! =NULL) {StreamReader reader=NewStreamReader (response. GetResponseStream (), Encoding.UTF8); Responsestr=Reader. ReadToEnd (); Reader. Close (); } } Catch(Exception) {Throw; } finally{Request=NULL; Requeststream=NULL; Response=NULL; } returnResponsestr; }Post Submission
Public Static stringHttpPost (stringUrlstringparam) {HttpWebRequest Request=(HttpWebRequest) httpwebrequest.create (URL); Request. Method="POST"; Request. ContentType="application/x-www-form-urlencoded"; Request. Accept="*/*"; Request. Timeout=15000; Request. AllowAutoRedirect=false; StreamWriter Requeststream=NULL; WebResponse response=NULL; stringResponsestr =NULL; Try{requeststream=NewStreamWriter (Request. GetRequestStream ()); requestStream.Write (param); Requeststream.close (); Response=request. GetResponse (); if(Response! =NULL) {StreamReader reader=NewStreamReader (response. GetResponseStream (), Encoding.UTF8); Responsestr=Reader. ReadToEnd (); Reader. Close (); } } Catch(Exception) {Throw; } finally{Request=NULL; Requeststream=NULL; Response=NULL; } returnResponsestr; }Get Commit
Public platform call Post submit data some need to pass data in JSON format if the data is not in JSON format may be error
2. Simple Sample Code
/// <summary> ///serializing a dictionary type to a JSON string/// </summary> /// <typeparam name= "TKey" >Dictionary key</typeparam> /// <typeparam name= "TValue" >Dictionary value</typeparam> /// <param name= "Dict" >the dictionary data to serialize</param> /// <returns>JSON string</returns> Public Static stringSerializedictionarytojsonstring<tkey, Tvalue> (Dictionary<tkey, tvalue>dict) { if(Dict. Count = =0) return ""; stringJsonstr =Jsonconvert.serializeobject (dict); returnJsonstr; }simply serialize the dictionary type to a JSON string
PublicActionResult Index () {Dictionary<string,string> list =Newdictionary<string,string>(); List. ADD ("begin_date","2014-12-07"); List. ADD ("end_date","2014-12-07"); //The user analysis data interface starts with data that is not delivered in JSON format and is always quoted in a format that is not correct. Simply write a JSON conversion varstr = Utils.httppost ("https://api.weixin.qq.com/datacube/getusersummary?access_token=xxxxxxxx", serializedictionarytojsonstring (list)); returnView ();} User Analytics Data Interface
3. Access to Web page information-can be used to make a file more than use (Background results announcement content, by getting the results of the announcement content crawl to the foreground display)
#regionGet Web page Information#regionGet information based on URL address get Public StaticString GetResult (stringURL) { returngetresult (URL, encoding.default); } #endregion #regionObtain the corresponding encoding information according to the URL address Public StaticString GetResult (stringURL, Encoding Encoding) { Try { varWebRequest =(HttpWebRequest) webrequest.create (URL); //Get response Results varWebResponse =(HttpWebResponse) webrequest.getresponse (); //Get response FlowStream stream =WebResponse.GetResponseStream (); using(StreamReader StreamReader =NewStreamReader (stream, encoding)) { stringresult =String.Empty; Result=Streamreader.readtoend (); Streamreader.close (); WebResponse.Close (); Webrequest.abort (); returnresult; } } Catch(Exception ex) {return "-1"; //return ex. Message.length > 100? Ex. Message.substring (0, +): Ex. Message; } } #endregion #endregionget web page information
/// <summary> ///get and intercept HTML source code/// </summary> /// <param name= "strURL" ></param> /// <returns></returns> Public Static stringGeturlandcutsourcehtml (stringstrURL) { stringstrtemp = Common.getresult (strURL, System.Text.Encoding.GetEncoding ("Utf-8")); if(!strtemp.equals ("-1") ) {strtemp= Strtemp.substring (Strtemp.indexof ("<!--begin-->")); strtemp= Strtemp.substring (0, Strtemp.indexof ("<!--end-->")); } returnstrtemp; }get the content between
<body> <form> <!--begin--> <div> What is displayed displays content The content displayed content displays content displays content displays content displays content display contents </div> <!--end--> </form></body>
HTML pageC # background Get, Post network request