Post calls the interface and passes json parameters
public string Post(string Url, string jsonParas) { string strURL = Url; //Create an HTTP request HttpWebRequest request = (HttpWebRequest)(strURL); //Post request method = "POST"; //Content Type = "application/json"; //Set parameters and perform URL encoding string paraUrlCoded = jsonParas;//(jsonParas); byte[] payload; //Convert Json string to bytes payload = .(paraUrlCoded); //Set the requested ContentLength = ; //Send a request and get the request flow Stream writer; try { writer = ();//Get the Stream object used to write the request data } catch (Exception) { writer = null; ("Connecting to the server failed!"); } //Write request parameters to stream (payload, 0, ); ();//Close the request flow // String strValue = "";// strValue is the character stream returned by the http response HttpWebResponse response; try { //Get the response flow response = (HttpWebResponse)(); } catch (WebException ex) { response = as HttpWebResponse; } Stream s = (); // Stream postData = ; StreamReader sRead = new StreamReader(s); string postContent = (); (); return postContent;//Return Json data } //Call: string askurl = testUrl + "?s&mobi=" + mobi + "&sign=" + sign + "&msg=" + encodeMsgs; //string relust = Post(askurl, ""); // Or string relust = Post(askurl, s&mobi=" + mobi + "&sign=" + sign + "&msg=" + encodeMsgs);
Post request parameters (send Json parameters, send normal parameters)
1. Send a Post request in Json format
(1) Post call
string PostUrl="http://IP:Port/AAA/BBB/CCC";string ; string Name="Little Black"; JObject patientinfo = new JObject(); JArray ids = new JArray(); (id); patientinfo["ids"] = ids; patientinfo["Name"] = Name; string sendData = (patientinfo); //eg: The format required to send Url: sendData={"ids":[123],"Name": Xiaohei}string resultData = Post(sendData, PostUrl);
(2) Post method
#region Post Request/// <summary> /// http Post request /// </summary> /// <param name="parameterData">parameter</param> /// <param name="serviceUrl">Access Address</param> /// <param name="ContentType">Default application/json , application/x-www-form-urlencoded,multipart/form-data,raw,binary </param> /// <param name="Accept">Default application/json</param> /// <returns></returns> public string Post(string parameterData, string serviceUrl, string ContentType = "application/json", string Accept = "application/json") { //First construct the request address based on the uri requested by the user //string serviceUrl = ("{0}/{1}", , uri); //Create a Web access object HttpWebRequest myRequest = (HttpWebRequest)(serviceUrl); //Convert the data sent by the user into a byte stream of "UTF-8" byte[] buf = ("UTF-8").GetBytes(parameterData); = "POST"; // = "application/json"; // = "application/json"; // //Content-Type: application/x-www-form-urlencoded = ; = Accept; // = ContentType; = "application/json; charset=UTF-8"; = ; = 1; = true; //("content-type", "application/json"); //("accept-encoding", "gzip"); //("accept-charset", "utf-8"); //Send a request Stream stream = (); (buf, 0, ); (); //Get response content through web access object HttpWebResponse myResponse = (HttpWebResponse)(); //Create StreamReader object by responding to content streams, because StreamReader is more advanced and faster StreamReader reader = new StreamReader((), Encoding.UTF8); //string returnXml = (());//If there is any encoding problem, use this method string returnData = ();//Use StreamReader to read from the beginning to the end of the response content (); (); return returnData; } #endregion
2. Post request with parameters
(1) Post call
string PostUrl="http://IP:Port/AAA/BBB/CCC";Dictionary<string, string> dic=new Dictionary<string, string>(); ("Id","123"); ("Name","Xiaoxiao"); string resultData = Post(dic, PostUrl);
(2) Post method
#region post request with parameters /// <summary> /// Specify the Post address to get all strings using Get /// Post is to transfer data from the server /// </summary> /// <param name="url">Request Url address</param> /// <param name="dic">Split Url fields</param> /// <returns></returns> public static string Post(string url, Dictionary<string, string> dic) { #region 【online checked】 //eg: http://IP:Port/AAA/BBB/CCC//Send corresponding parameters #region Create a Web Access Object HttpWebRequest req = (HttpWebRequest)(url); // = "POST"; // = "application/x-www-form-urlencoded"; = "POST"; = ; = "application/json; charset=UTF-8"; #endregion #region Post adds and splices parameters to form the corresponding Url address StringBuilder builder = new StringBuilder(); int i = 0; if ( > 0) { foreach (var item in dic) { if (i > 0) ("&"); ("{0}={1}", , ); i++; } } #endregion #region Send a request byte[] data = Encoding.(()); = ; using (Stream reqStream = ()) { (data, 0, ); (); } #endregion #region Get response content through web access string result = ""; HttpWebResponse resp = (HttpWebResponse)(); Stream stream = (); //Get response content using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = (); } return result; #endregion #endregion } #endregion
The above is personal experience. I hope you can give you a reference and I hope you can support me more.