您的位置 首页 > 娱乐休闲

C井利用HttpWebRequest进行post请求的示例(HTTPS)

阅读数:59595

代码如下:


  1. using System;
  2. using Sy;
  3. using Sy;
  4. using Sy;
  5. using Sy;
  6. using Sy;
  7. using Sy;
  8. using Sy;
  9. using Sy.Compression;
  10. using Sy.RegularExpressions;
  11. namespace HttpWebrequestDemo
  12. {
  13. class Program
  14. {
  15. private static readonly string DefaultUserAgent = "Mozilla (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
  16. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  17. {
  18. return true; //总是接受
  19. }
  20. public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters,Encoding charset)
  21. {
  22. HttpWebRequest request = null;
  23. //HTTPSQ请求
  24. ServicePoin = new RemoteCertificateValidationCallback(CheckValidationResult);
  25. request = WebReque(url) as HttpWebRequest;
  26. reque = H;
  27. reque = "POST";
  28. reque = "application/x-www-form-urlencoded";
  29. reque = DefaultUserAgent;
  30. //如果需要POST数据
  31. if (!(parameters == null || == 0))
  32. {
  33. StringBuilder buffer = new StringBuilder();
  34. int i = 0;
  35. foreach (string key in )
  36. {
  37. if (i > 0)
  38. {
  39. bu("&{0}={1}", key, parameters[key]);
  40. }
  41. else
  42. {
  43. bu("{0}={1}", key, parameters[key]);
  44. }
  45. i++;
  46. }
  47. byte[] data = c());
  48. using (Stream stream = reque())
  49. {
  50. (data, 0, da);
  51. }
  52. }
  53. return reque() as HttpWebResponse;
  54. }
  55. static void Main(string[] args)
  56. {
  57. string url = ";;
  58. Encoding encoding = Encoding.GetEncoding("utf-8");
  59. IDictionary<string, string> parameters = new Dictionary<string, string>();
  60. ("authuser", "*****");
  61. ("authpass", "*****");
  62. ("orgkey","*****");
  63. ("orgname", "*****");
  64. HttpWebResponse response = Program.CreatePostHttpResponse(url,parameters,encoding);
  65. //打印返回值
  66. Stream stream = re(); //获取响应的字符串流
  67. StreamReader sr = new StreamReader(stream); //创建一个stream读取流
  68. string html = (); //从头读到尾,放到字符串html
  69. Con(html);
  70. }
  71. }
  72. }

HttpWebRequest默认会用代理进行连接,导致获取结果比较慢。解决办法是,配置:

reque = null;

不使用代理,即可。

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“网页如何写post提交数据,网页post提交表单,如何获取post提交的内容,post方法如何提交数据”边界阅读