In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how C # sends Get and Post requests". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how C # sends Get and Post requests".
Catalogue
Get request
Post request
Get request
1. Simply send a Get request
/ / specify the Url address and use Get to get all strings / public static string Get (string url) {string result = ""; HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url); HttpWebResponse resp = (HttpWebResponse) req.GetResponse (); Stream stream = resp.GetResponseStream () Try {/ / get content using (StreamReader reader = new StreamReader (stream)) {result = reader.ReadToEnd ();}} finally {stream.Close ();} return result;}
two。 Get method with request parameters
/ send Get request / address / request parameter definition / public static string Get (string url, Dictionary dic) {string result = ""; StringBuilder builder = new StringBuilder (); builder.Append (url); if (dic.Count > 0) {builder.Append ("?"); int I = 0 Foreach (var item in dic) {if (I > 0) builder.Append ("&"); builder.AppendFormat ("{0} = {1}", item.Key, item.Value); iTunes;} HttpWebRequest req = (HttpWebRequest) WebRequest.Create (builder.ToString ()); / / add parameter HttpWebResponse resp = (HttpWebResponse) req.GetResponse () Stream stream = resp.GetResponseStream (); try {/ / get content using (StreamReader reader = new StreamReader (stream)) {result = reader.ReadToEnd ();}} finally {stream.Close ();} return result;}
3. Custom specified Http request headers, custom specified encoding parsing returned results
HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url); req.Method = "GET"; req.Headers ["Accept-Language"] = "zh-CN,zh;q=0.8"; req.Referer = "https://www.baidu.com/";HttpWebResponse resp = (HttpWebResponse) req.GetResponse (); Stream stream = resp.GetResponseStream (); string result =" / / Note that the code used here is: gb2312//using (StreamReader reader = new StreamReader (stream, Encoding.Default)) using (StreamReader reader = new StreamReader (stream, Encoding.GetEncoding ("gb2312") {result = reader.ReadToEnd ();} Post request
The content transmission here is encoded in UTF-8.
1. Send a Post request without parameters
/ / specify the Post address and use Get to get all strings / public static string Post (string url) {string result = ""; HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url); req.Method = "POST" HttpWebResponse resp = (HttpWebResponse) req.GetResponse (); Stream stream = resp.GetResponseStream (); / / get content using (StreamReader reader = new StreamReader (stream, Encoding.UTF8)) {result = reader.ReadToEnd ();} return result;}
two。 Post request with parameters, specifying key-value pairs
/ / specify the Post address and use Get to get all strings / public static string Post (string url,Dictionary dic) {string result = ""; HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url); req.Method = "POST" Req.ContentType = "application/x-www-form-urlencoded"; # region add Post parameter StringBuilder builder = new StringBuilder (); int I = 0; foreach (var item in dic) {if (I > 0) builder.Append ("&") Builder.AppendFormat ("{0} = {1}", item.Key, item.Value); iTunes;} byte [] data = Encoding.UTF8.GetBytes (builder.ToString ()); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream ()) {reqStream.Write (data, 0, data.Length) ReqStream.Close ();} # endregion HttpWebResponse resp = (HttpWebResponse) req.GetResponse (); Stream stream = resp.GetResponseStream (); / / get response content using (StreamReader reader = new StreamReader (stream, Encoding.UTF8)) {result = reader.ReadToEnd () } return result;}
3. Post request with parameters, specifying the contents of the send string
/ / specify Post address to get all strings using Get method / / request backend address / Post submit data content (utf-8 encoded) / public static string Post (string url, string content) {string result = ""; HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded" # region add Post parameters byte [] data = Encoding.UTF8.GetBytes (content); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream ()) {reqStream.Write (data, 0, data.Length); reqStream.Close ();} # endregion HttpWebResponse resp = (HttpWebResponse) req.GetResponse (); Stream stream = resp.GetResponseStream () / / get the response content using (StreamReader reader = new StreamReader (stream, Encoding.UTF8)) {result = reader.ReadToEnd ();} return result;}
Reprint method:
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create ("http://fanyi.baidu.com/transcontent"); Encoding encoding = Encoding.UTF8; string param =" ie=utf-8&source=txt&query=hello&t=1327829764203&token=8a7dcbacb3ed72cad9f3fb079809a127&from=auto&to=auto "; / / encoding.GetBytes (postData); byte [] bs = Encoding.ASCII.GetBytes (param); string responseData = String.Empty Req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream ()) {reqStream.Write (bs, 0, bs.Length); reqStream.Close () } using (HttpWebResponse response = (HttpWebResponse) req.GetResponse ()) {using (StreamReader reader = new StreamReader (response.GetResponseStream (), encoding)) {responseData = reader.ReadToEnd (). ToString ();} context.Response.Write (responseData) } Thank you for reading. The above is the content of "how to send Get and Post requests for C#". After the study of this article, I believe you have a deeper understanding of how C# sends Get and Post requests, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.