请选择 进入手机版 | 继续访问电脑版

[C#教程] C#发送Get、Post哀求 (带参数)

[复制链接]
查看44 | 回复6 | 2021-9-14 12:50:42 | 显示全部楼层 |阅读模式
目次

Get哀求

1.简单发送Get哀求

  1. /// <summary>
  2. /// 指定Url地址使用Get 方式获取全部字符串
  3. /// </summary>
  4. /// <param name="url">请求链接地址</param>
  5. /// <returns></returns>
  6. public static string Get(string url)
  7. {
  8. string result = "";
  9. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  10. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  11. Stream stream = resp.GetResponseStream();
  12. try
  13. {
  14. //获取内容
  15. using (StreamReader reader = new StreamReader(stream))
  16. {
  17. result = reader.ReadToEnd();
  18. }
  19. }
  20. finally
  21. {
  22. stream.Close();
  23. }
  24. return result;
  25. }
复制代码

2.带哀求 参数的Get方法

  1. /// <summary>
  2. /// 发送Get请求
  3. /// </summary>
  4. /// <param name="url">地址</param>
  5. /// <param name="dic">请求参数定义</param>
  6. /// <returns></returns>
  7. public static string Get(string url, Dictionary<string, string> dic)
  8. {
  9. string result = "";
  10. StringBuilder builder = new StringBuilder();
  11. builder.Append(url);
  12. if (dic.Count > 0)
  13. {
  14. builder.Append("?");
  15. int i = 0;
  16. foreach (var item in dic)
  17. {
  18. if (i > 0)
  19. builder.Append("&");
  20. builder.AppendFormat("{0}={1}", item.Key, item.Value);
  21. i++;
  22. }
  23. }
  24. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(builder.ToString());
  25. //添加参数
  26. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  27. Stream stream = resp.GetResponseStream();
  28. try
  29. {
  30. //获取内容
  31. using (StreamReader reader = new StreamReader(stream))
  32. {
  33. result = reader.ReadToEnd();
  34. }
  35. }
  36. finally
  37. {
  38. stream.Close();
  39. }
  40. return result;
  41. }
复制代码

3.自定义指定Http哀求 头,自定义指定编码分析 返回结果

  1. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  2. req.Method = "GET";
  3. req.Headers["Accept-Language"] = "zh-CN,zh;q=0.8";
  4. req.Referer = "https://www.baidu.com/";
  5. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  6. Stream stream = resp.GetResponseStream();
  7. string result = "";
  8. //注意,此处使用的编码是:gb2312
  9. //using (StreamReader reader = new StreamReader(stream, Encoding.Default))
  10. using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gb2312")))
  11. {
  12. result = reader.ReadToEnd();
  13. }
复制代码

Post哀求

此处内容传输都是用UTF-8编码

1.不带参数发送Post哀求

  1. /// <summary>
  2. /// 指定Post地址使用Get 方式获取全部字符串
  3. /// </summary>
  4. /// <param name="url">请求后台地址</param>
  5. /// <returns></returns>
  6. public static string Post(string url)
  7. {
  8. string result = "";
  9. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  10. req.Method = "POST";
  11. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  12. Stream stream = resp.GetResponseStream();
  13. //获取内容
  14. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  15. {
  16. result = reader.ReadToEnd();
  17. }
  18. return result;
  19. }
复制代码

2.带参数Post哀求 ,指定键值对

  1. /// <summary>
  2. /// 指定Post地址使用Get 方式获取全部字符串
  3. /// </summary>
  4. /// <param name="url">请求后台地址</param>
  5. /// <returns></returns>
  6. public static string Post(string url,Dictionary<string,string> dic)
  7. {
  8. string result = "";
  9. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  10. req.Method = "POST";
  11. req.ContentType = "application/x-www-form-urlencoded";
  12. #region 添加Post 参数
  13. StringBuilder builder = new StringBuilder();
  14. int i = 0;
  15. foreach (var item in dic)
  16. {
  17. if (i > 0)
  18. builder.Append("&");
  19. builder.AppendFormat("{0}={1}", item.Key, item.Value);
  20. i++;
  21. }
  22. byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
  23. req.ContentLength = data.Length;
  24. using (Stream reqStream = req.GetRequestStream())
  25. {
  26. reqStream.Write(data, 0, data.Length);
  27. reqStream.Close();
  28. }
  29. #endregion
  30. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  31. Stream stream = resp.GetResponseStream();
  32. //获取响应内容
  33. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  34. {
  35. result = reader.ReadToEnd();
  36. }
  37. return result;
  38. }
复制代码

3.带参数的Post哀求 ,指定发送字符串内容

  1. /// <summary>
  2. /// 指定Post地址使用Get 方式获取全部字符串
  3. /// </summary>
  4. /// <param name="url">请求后台地址</param>
  5. /// <param name="content">Post提交数据内容(utf-8编码的)</param>
  6. /// <returns></returns>
  7. public static string Post(string url, string content)
  8. {
  9. string result = "";
  10. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  11. req.Method = "POST";
  12. req.ContentType = "application/x-www-form-urlencoded";
  13. #region 添加Post 参数
  14. byte[] data = Encoding.UTF8.GetBytes(content);
  15. req.ContentLength = data.Length;
  16. using (Stream reqStream = req.GetRequestStream())
  17. {
  18. reqStream.Write(data, 0, data.Length);
  19. reqStream.Close();
  20. }
  21. #endregion
  22. HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
  23. Stream stream = resp.GetResponseStream();
  24. //获取响应内容
  25. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  26. {
  27. result = reader.ReadToEnd();
  28. }
  29. return result;
  30. }
复制代码

转载方法:

  1. HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://fanyi.baidu.com/transcontent");
  2. Encoding encoding = Encoding.UTF8;
  3. string param = "ie=utf-8&source=txt&query=hello&t=1327829764203&token=8a7dcbacb3ed72cad9f3fb079809a127&from=auto&to=auto";
  4. //encoding.GetBytes(postData);
  5. byte[] bs = Encoding.ASCII.GetBytes(param);
  6. string responseData = String.Empty;
  7. req.Method = "POST";
  8. req.ContentType = "application/x-www-form-urlencoded";
  9. req.ContentLength = bs.Length;
  10. using (Stream reqStream = req.GetRequestStream())
  11. {
  12. reqStream.Write(bs, 0, bs.Length);
  13. reqStream.Close();
  14. }
  15. using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
  16. {
  17. using (StreamReader reader = new StreamReader(response.GetResponseStream(),encoding))
  18. {
  19. responseData = reader.ReadToEnd().ToString();
  20. }
  21. context.Response.Write(responseData);
  22. }
复制代码

到此这篇关于C#发送Get、Post哀求 (带参数)的文章就先容 到这了,更多相干 C#发送Get、Post哀求 内容请搜刮 脚本之家从前 的文章或继续欣赏 下面的相干 文章盼望 大家以后多多支持脚本之家! 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

avatar 保佑缎 | 2021-9-22 03:33:35 | 显示全部楼层
admin楼主,我告诉你一个你不知道的的秘密,有一个牛逼的网站,运动刷步数还是免费刷的,QQ和微信都可以刷,特别好用。访问地址:http://yd.mxswl.com 猫先森网络
回复

使用道具 举报

avatar 123457839 | 2021-9-26 22:01:54 | 显示全部楼层
支持一下!
回复

使用道具 举报

avatar 江左岸右郧 | 2021-9-28 04:23:19 | 显示全部楼层
很多天不上线,一上线就看到这么给力的帖子!
回复

使用道具 举报

avatar 崔炫俊献 | 2021-9-29 19:40:15 | 显示全部楼层
知识就是力量啊!
回复

使用道具 举报

avatar 张小麦坡 | 2021-10-6 03:45:29 | 显示全部楼层
楼上的别说的那么悲观好吧!
回复

使用道具 举报

avatar 永远爱你冰塘 | 2021-10-6 05:08:18 | 显示全部楼层
顶!顶!顶!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则