USClientCommunicate.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.IO;
  6. using System.Web;
  7. using System.Threading;
  8. namespace US.Browser.IE.BLL
  9. {
  10. public class USClientCommunicate
  11. {
  12. #region 向其他网站请求数据
  13. /// <summary>
  14. /// 读取返回的请求的数据 get
  15. /// </summary>
  16. /// <param name="url"></param>
  17. /// <returns></returns>
  18. public static string GetPage(string url)
  19. {
  20. try
  21. {
  22. HttpWebRequest webReques = (HttpWebRequest)HttpWebRequest.Create(url);//创建一个网站连接
  23. webReques.Method = "GET"; //get 方式获得
  24. HttpWebResponse webRespon = (HttpWebResponse)webReques.GetResponse();//获取网站放回的信息
  25. System.IO.Stream newstream = webRespon.GetResponseStream();//读取服务端响应的流
  26. System.IO.StreamReader srRead = new System.IO.StreamReader(newstream, System.Text.Encoding.UTF8);//将网页响应的流转化成utf-8数据
  27. string outString = srRead.ReadToEnd();//读取数据
  28. srRead.Close();//关闭
  29. return outString;//返回
  30. }
  31. catch { return ""; }
  32. }
  33. /// <summary>
  34. /// 文件下载
  35. /// </summary>
  36. /// <param name="UUR"></param>
  37. /// <param name="UFN"></param>
  38. public static void DownloadFile(string UUR, string UFN)
  39. {
  40. try
  41. {
  42. System.Net.ServicePointManager.DefaultConnectionLimit = 100; System.GC.Collect();
  43. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UUR);
  44. request.Method = "get";//请求的类型 POST或者GET
  45. request.Headers.Add("Cache-Control", "no-cache");//设置无缓存
  46. HttpWebResponse response = (HttpWebResponse)request.GetResponse();//获取返回的值
  47. Stream stream = response.GetResponseStream();
  48. Stream st = new FileStream(UFN, System.IO.FileMode.Create);
  49. byte[] by = new byte[102400]; int cn; while ((cn = stream.Read(by, 0, by.Length)) > 0) { st.Write(by, 0, cn); }
  50. st.Close(); stream.Close(); st.Dispose(); stream.Dispose(); request.Abort(); response.Close();
  51. }
  52. catch (Exception e) { }
  53. finally { }
  54. }
  55. public delegate void AsynPost();
  56. /// <summary>
  57. /// 发送请求
  58. /// </summary>
  59. /// <param name="url"></param>
  60. /// <param name="method"></param>
  61. /// <param name="fun"></param>
  62. /// <param name="type"></param>
  63. /// <param name="param"></param>
  64. /// <returns></returns>
  65. public static string PostPage(string url, string method, System.Action<string> fun, string type, params string[] param)
  66. {
  67. try
  68. {
  69. method = method.ToUpper(); int i; string data = string.Empty; bool isPost = method == "POST"; fun = fun == null ? delegate(string e) { } : fun;
  70. for (i = 0; i < param.Length; i++) { param[i] = HttpUtility.UrlEncode(HttpUtility.UrlEncode(param[i], Encoding.UTF8).Replace("+", "%20")); }//获取参数
  71. if (param != null && param.Length != 0) { data = string.Join(",", param); } data = type + data; byte[] bytes = Encoding.UTF8.GetBytes(data);//生成传参
  72. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); //建立连接
  73. request.KeepAlive = false;//设置不长连接
  74. request.Timeout = 60000;//请求过期时间
  75. request.Method = method;//请求的类型 POST或者GET
  76. request.Headers.Add("Cache-Control", "no-cache");//设置无缓存
  77. request.ContentType = "application/x-www-form-urlencoded";//请求传参的类型 这里为FORM传参
  78. if (method == "POST") request.ContentLength = bytes.Length; request.Proxy = null;//请求的长度
  79. if (fun != null) //异步回调
  80. {
  81. request.BeginGetRequestStream(new AsyncCallback(delegate(IAsyncResult result) //请求可写入数据
  82. {
  83. try
  84. {
  85. HttpWebRequest re = (HttpWebRequest)result.AsyncState;
  86. if (method == "POST" && bytes.Length > 0)
  87. {
  88. using (Stream requestStream = request.EndGetRequestStream(result)) //写入数据
  89. {
  90. requestStream.Write(bytes, 0, bytes.Length); requestStream.Close();
  91. }
  92. }
  93. request.BeginGetResponse(new AsyncCallback(delegate(IAsyncResult res) { try { fun(ReadCallback((HttpWebRequest)res.AsyncState)); } catch (Exception e) { BLL.USIE.Exception(null, e); } }), request); ; //获取返回值
  94. }
  95. catch (Exception e) { BLL.USIE.Exception(null, e); }
  96. }), request);
  97. }
  98. else
  99. {
  100. if (isPost)
  101. {
  102. using (Stream requestStream = request.GetRequestStream())
  103. {
  104. requestStream.Write(bytes, 0, bytes.Length); requestStream.Close();
  105. }
  106. }
  107. return ReadCallback(request);
  108. }
  109. }
  110. catch (Exception e) { BLL.USIE.Exception(null, e); }
  111. return "";
  112. }
  113. /// <summary>
  114. /// 异步处理
  115. /// </summary>
  116. /// <param name="asynchronousResult"></param>
  117. private static string ReadCallback(HttpWebRequest request)
  118. {
  119. HttpWebResponse response = (HttpWebResponse)request.GetResponse();//获取返回的值
  120. if (response.StatusCode == HttpStatusCode.OK)
  121. {
  122. using (Stream stream = response.GetResponseStream())
  123. {
  124. System.IO.StreamReader srRead = new System.IO.StreamReader(stream, Encoding.UTF8);//将网页响应的流转化成utf-8数据
  125. string _STR = srRead.ReadToEnd(); stream.Close(); request.Abort(); response.Close(); return _STR;
  126. }
  127. }
  128. return string.Empty;
  129. }
  130. #endregion
  131. }
  132. }