Code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Text;usingSystem.Net;usingSystem.IO;usingSystem.Security.Cryptography;namespacetest{ Public Partial classWebForm3:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { //string url = "http://111.206.37.135: 8080/business/api/ordercreate "; //string paras = "createtime%3d2014-12-16+11%3a07%3a52%26orderlist%3d%5b%7bagentname%3a%e6%90%ba%e7%a8%8b% 2citemamount%3a1%2citemid%3a265802%2citemimgurl%3a%2citemname%3a%e6%b2%99%e7%89%b9%e9%a6%86%e2%80%9c%e6%9c%88% E4%ba%ae%e8%88%b9%e2%80%9d%e5%95%86%e5%8a%a1vip%e4%b8%93%e4%ba%ab%e6%9c%8d%e5%8a%a1%2citemordertime% 3a2014-12-25%2citemprice%3a12000.00%2citemurl%3ahttp%3a%2f%2fpiao.ctrip.com%2fdest%2ft138822.html%2citemurlwap %3ahttp%3a%2f%2fm.ctrip.com%2fwebapp%2fticket%2fdest%2ft138822.html%7d%5d%26ordername%3d%e4%b8%8a%e6%b5%b7%e4% B8%96%e5%8d%9a%e4%bc%9a%e6%b2%99%e7%89%b9%e9%a6%86%e2%80%9c%e6%9c%88%e4%ba%ae%e8%88%b9%e2%80%9dvip%e6%9c%8d%e5 %8a%a1%26partnerid%3dctrip%26payway%3d%e5%9c%a8%e7%ba%bf%e6%94%af%e4%bb%98%26srcorderid%3d1143395377%26status% 3d104%26totalamount%3d12000.00%26sign%3db9821727999bce3ab9748a2c332cbab "; //string result = Sendbypost (URL, paras); stringURL ="http://www.baidu.com"; stringresult =sendbyget (URL); } /// <summary> ///Post mode submit Request/// </summary> /// <param name= "url" ></param> /// <param name= "result" ></param> /// <returns></returns> Public stringSendbypost (stringUrlstringparas) { stringresult =string. Empty; Try{System.Net.HttpWebRequest request; Request=(System.Net.HttpWebRequest) webrequest.create (URL); //POST Request ModeRequest. Method ="POST"; //Content TypeRequest. ContentType ="application/x-www-form-urlencoded"; //parameters are URL encoded//string paraurlcoded = System.Web.HttpUtility.UrlEncode ("keyword"); //paraurlcoded + = "=" + System.Web.HttpUtility.UrlEncode ("Multi-month"); byte[] payload; //Converts a URL-encoded string into bytesPayload =System.Text.Encoding.UTF8.GetBytes (paras); //set the contentlength of the requestRequest. ContentLength =payload. Length; //GET Request FlowSystem.IO.Stream writer =request. GetRequestStream (); //Write request parameters to streamWriter. Write (Payload,0, payload. Length); //close the request flowwriter. Close (); System.Net.HttpWebResponse response; //get the response streamResponse =(System.Net.HttpWebResponse) request. GetResponse (); System.IO.StreamReader myreader=NewSystem.IO.StreamReader (response. GetResponseStream (), Encoding.UTF8); Result=myreader. ReadToEnd (); myreader. Close (); } Catch(Exception ex) {result=Ex. ToString (); } returnresult; } /// <summary> ///Get method Submit Request/// </summary> /// <param name= "url" >Note URLs must contain HTTP or HTTPS</param> /// <returns></returns> Public stringSendbyget (stringURL) { stringresult =string. Empty; Try { //System.Net.HttpWebRequest request; ////Create an HTTP request //request = (System.Net.HttpWebRequest) webrequest.create (URL); ////request. Method= "Get"; //System.Net.HttpWebResponse response; //response = (System.Net.HttpWebResponse) request. GetResponse (); //System.IO.StreamReader myreader = new System.IO.StreamReader (response. GetResponseStream (), Encoding.UTF8); //result = myreader. ReadToEnd (); //myreader. Close ();HttpWebRequest req=(HttpWebRequest) webrequest.create (URL); Req. Method="GET"; ////Set up agents //if (commonfunc.convertobjecttostring (configurationmanager.appsettings["IsProxy"]). ToUpper () = = "T")//{ //string proxyaddress = configurationmanager.appsettings["proxyaddress"]. ToString (). Trim (); //if (!string. IsNullOrEmpty (proxyaddress))// { //req. useDefaultCredentials = true; //WebProxy proxy = new WebProxy (proxyaddress, 8080); //req. Proxy = proxy; // } //logger. Debug ("proxy", "Set up Proxy service"); //}Req. KeepAlive =true; Req. Timeout=300000; Req. ContentType="Application/x-www-form-urlencoded;charset=utf-8"; HttpWebResponse RSP=(HttpWebResponse) req. GetResponse (); Encoding Encoding=NULL; Try{encoding=encoding.getencoding (RSP. CharacterSet); } Catch{encoding=Encoding.UTF8; } Stream Stream=RSP. GetResponseStream (); StreamReader Reader=NewStreamReader (stream, encoding); Result=Reader. ReadToEnd (); Reader. Close (); Stream. Close (); Rsp. Close (); } Catch(Exception ex) {result=Ex. Message; } returnresult; } }}
Get,post Submit Request