1. The format of parameter paramsvalue should be consistent with Reques.contenttype,
If Contentype "application/x-www-form-urlencoded" form type, then the parameter is a=1&b=2 form
If... "Application/json" JSON type then the parameter is "{a:1,b:2}" format
2. You can add a custom header, add (Key,value)
Accept Get header Request.Headers.Get (key)
Get
Public Static stringHttpGet (stringURL) { stringresult=string. Empty; Try{HttpWebRequest Wbrequest=(HttpWebRequest) webrequest.create (URL); Wbrequest.method="GET"; HttpWebResponse Wbresponse=(HttpWebResponse) wbrequest.getresponse (); using(Stream Responsestream =Wbresponse.getresponsestream ()) { using(StreamReader Sreader =NewStreamReader (Responsestream)) {Result=Sreader.readtoend (); } } } Catch(Exception ex) {}returnresult; }
Post Request
Public Static stringHttpPost (stringUrlstringParamdata, dictionary<string,string> headerdic =NULL) { stringresult =string. Empty; Try{HttpWebRequest Wbrequest=(HttpWebRequest) webrequest.create (URL); Wbrequest.method="POST"; Wbrequest.contenttype="application/x-www-form-urlencoded"; Wbrequest.contentlength=Encoding.UTF8.GetByteCount (Paramdata); if(Headerdic! =NULL&& Headerdic.count >0) { foreach(varIteminchheaderdic) {WBREQUEST.HEADERS.ADD (item. Key, item. Value); } } using(Stream Requeststream =Wbrequest.getrequeststream ()) { using(StreamWriter swrite =NewStreamWriter (Requeststream)) {Swrite. Write (Paramdata); }} HttpWebResponse Wbresponse=(HttpWebResponse) wbrequest.getresponse (); using(Stream Responsestream =Wbresponse.getresponsestream ()) { using(StreamReader sread =NewStreamReader (Responsestream)) {Result=sread. ReadToEnd (); } } } Catch(Exception ex) {}returnresult; }
C # sends HTTP protocol to impersonate a Post GET request