C#發送Get、Post請求(帶參數)
Get請求
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(); }
Post請求
此處內容傳輸都是用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請求內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- C#使用Post調用接口並傳遞json參數
- C#通過GET/POST方式發送Http請求
- C#獲取微信小程序的雲數據庫中數據的示例代碼
- C#中的HttpWebRequest類用法詳解
- C#中的WebRequest與WebResponse抽象類、DNS靜態類、Ping類介紹