您好,登錄后才能下訂單哦!
這篇文章主要講解了“C#怎么發送Get、Post請求”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“C#怎么發送Get、Post請求”吧!
Get請求
Post請求
1.簡單發送Get請求
/// <summary> /// 指定Url地址使用Get 方式獲取全部字符串 /// </summary> /// <param name="url">請求鏈接地址</param> /// <returns></returns> public static string Get(string url) { string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); try { //獲取內容 using (StreamReader reader = new StreamReader(stream)) { result = reader.ReadToEnd(); } } finally { stream.Close(); } return result; }
2.帶請求參數的Get方法
/// <summary> /// 發送Get請求 /// </summary> /// <param name="url">地址</param> /// <param name="dic">請求參數定義</param> /// <returns></returns> public static string Get(string url, Dictionary<string, string> 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); i++; } } HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString()); //添加參數 HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); try { //獲取內容 using (StreamReader reader = new StreamReader(stream)) { result = reader.ReadToEnd(); } } finally { stream.Close(); } return result; }
3.自定義指定Http請求頭,自定義指定編碼解析返回結果
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 = ""; //注意,此處使用的編碼是:gb2312 //using (StreamReader reader = new StreamReader(stream, Encoding.Default)) using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gb2312"))) { result = reader.ReadToEnd(); }
此處內容傳輸都是用UTF-8編碼
1.不帶參數發送Post請求
/// <summary> /// 指定Post地址使用Get 方式獲取全部字符串 /// </summary> /// <param name="url">請求后臺地址</param> /// <returns></returns> 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(); //獲取內容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; }
2.帶參數Post請求,指定鍵值對
/// <summary> /// 指定Post地址使用Get 方式獲取全部字符串 /// </summary> /// <param name="url">請求后臺地址</param> /// <returns></returns> public static string Post(string url,Dictionary<string,string> dic) { string result = ""; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; #region 添加Post 參數 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); i++; } 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(); //獲取響應內容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; }
3.帶參數的Post請求,指定發送字符串內容
/// <summary> /// 指定Post地址使用Get 方式獲取全部字符串 /// </summary> /// <param name="url">請求后臺地址</param> /// <param name="content">Post提交數據內容(utf-8編碼的)</param> /// <returns></returns> 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 添加Post 參數 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(); //獲取響應內容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; }
轉載方法:
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); }
感謝各位的閱讀,以上就是“C#怎么發送Get、Post請求”的內容了,經過本文的學習后,相信大家對C#怎么發送Get、Post請求這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。