FormPost.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. namespace MOKA_Factory_Tools
  7. {
  8. /// <summary>
  9. /// 表单post
  10. /// </summary>
  11. class FormPost
  12. {
  13. /// <summary>
  14. /// post数据
  15. /// </summary>
  16. /// <param name="strUrl"></param>
  17. /// <param name="postParaList"></param>
  18. /// <param name="isFile">true:用multipart/form-data发送,false:默认格式</param>
  19. /// <returns></returns>
  20. public static string postMessage(string strUrl, int timeout,List<PostDateClass> postParaList, bool isFile = false)
  21. {
  22. if (isFile == true)
  23. {
  24. bool status;
  25. return postFileMessage(strUrl, postParaList,out status, timeout);
  26. }
  27. else
  28. {
  29. StringBuilder strPost = new StringBuilder();
  30. for (int i = 0; i < postParaList.Count; i++)
  31. {
  32. if (i != 0)
  33. {
  34. strPost.Append("&");
  35. }
  36. strPost.Append(postParaList[i].Prop);
  37. strPost.Append("=");
  38. strPost.Append(postParaList[i].Value);
  39. }
  40. return postMessage(strUrl, strPost.ToString());
  41. }
  42. }
  43. public static string postFileMessage(string strUrl, List<PostDateClass> postParaList,out bool status,int timeout)
  44. {
  45. try
  46. {
  47. status = true;
  48. string responseContent = "";
  49. var memStream = new MemoryStream();
  50. var webRequest = (HttpWebRequest)WebRequest.Create(strUrl);
  51. // 边界符
  52. var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
  53. // 边界符
  54. var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
  55. // 最后的结束符
  56. var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
  57. memStream.Write(beginBoundary, 0, beginBoundary.Length);
  58. // 设置属性
  59. webRequest.Method = "POST";
  60. webRequest.Timeout = timeout;
  61. webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
  62. webRequest.Proxy = null;
  63. for (int i = 0; i < postParaList.Count; i++)
  64. {
  65. PostDateClass temp = postParaList[i];
  66. if (temp.Type == 1)
  67. {
  68. var fileStream = new FileStream(temp.Value, FileMode.Open, FileAccess.Read);
  69. // 写入文件
  70. const string filePartHeader =
  71. "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
  72. "Content-Type: application/octet-stream\r\n\r\n";
  73. var header = string.Format(filePartHeader, temp.Prop, temp.Value);
  74. var headerbytes = Encoding.UTF8.GetBytes(header);
  75. memStream.Write(headerbytes, 0, headerbytes.Length);
  76. var buffer = new byte[1024];
  77. int bytesRead; // =0
  78. while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
  79. {
  80. memStream.Write(buffer, 0, bytesRead);
  81. }
  82. string end = "\r\n";
  83. headerbytes = Encoding.UTF8.GetBytes(end);
  84. memStream.Write(headerbytes, 0, headerbytes.Length);
  85. fileStream.Close();
  86. }
  87. else if (temp.Type == 0)
  88. {
  89. // 写入字符串的Key
  90. var stringKeyHeader = "Content-Disposition: form-data; name=\"{0}\"" +
  91. "\r\n\r\n{1}\r\n";
  92. var header = string.Format(stringKeyHeader, temp.Prop, temp.Value);
  93. var headerbytes = Encoding.UTF8.GetBytes(header);
  94. memStream.Write(headerbytes, 0, headerbytes.Length);
  95. }
  96. if (i != postParaList.Count - 1)
  97. memStream.Write(beginBoundary, 0, beginBoundary.Length);
  98. else
  99. // 写入最后的结束边界符
  100. memStream.Write(endBoundary, 0, endBoundary.Length);
  101. }
  102. webRequest.KeepAlive = false;
  103. System.Net.ServicePointManager.DefaultConnectionLimit = 100;
  104. //webRequest.ContentLength = memStream.Length;
  105. var requestStream = webRequest.GetRequestStream();
  106. memStream.Position = 0;
  107. var tempBuffer = new byte[memStream.Length];
  108. memStream.Read(tempBuffer, 0, tempBuffer.Length);
  109. memStream.Close();
  110. requestStream.Write(tempBuffer, 0, tempBuffer.Length);
  111. requestStream.Close();
  112. using (HttpWebResponse res = (HttpWebResponse)webRequest.GetResponse())
  113. {
  114. using (Stream resStream = res.GetResponseStream())
  115. {
  116. byte[] buffer = new byte[1024];
  117. int read;
  118. while ((read = resStream.Read(buffer, 0, buffer.Length)) > 0)
  119. {
  120. responseContent += Encoding.UTF8.GetString(buffer, 0, read);
  121. }
  122. }
  123. res.Close();
  124. }
  125. return responseContent;
  126. }
  127. catch (Exception e)
  128. {
  129. status = false;
  130. GC.Collect();
  131. return e.Message;
  132. }
  133. }
  134. public static string postMessage(string strUrl, string strPost)
  135. {
  136. try
  137. {
  138. CookieContainer objCookieContainer = null;
  139. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
  140. request.Method = "Post";
  141. request.Accept = "*/*";
  142. request.Headers.Add("Accept-Language: zh-CN,zh;q=0.8");
  143. request.Headers.Add("Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3");
  144. request.ContentType = "application/x-www-form-urlencoded";
  145. request.Timeout = 10000;
  146. request.Referer = strUrl;//.Remove(strUrl.LastIndexOf("/"));
  147. Console.WriteLine(strUrl);
  148. if (objCookieContainer == null)
  149. objCookieContainer = new CookieContainer();
  150. request.CookieContainer = objCookieContainer;
  151. //Console.WriteLine(objCookieContainer.ToString());
  152. if (!string.IsNullOrEmpty(strPost))
  153. {
  154. byte[] byteData = Encoding.UTF8.GetBytes(strPost.ToString().TrimEnd('&'));
  155. request.ContentLength = byteData.Length;
  156. using (Stream reqStream = request.GetRequestStream())
  157. {
  158. reqStream.Write(byteData, 0, byteData.Length);
  159. reqStream.Close();
  160. }
  161. }
  162. string strResponse = "";
  163. using (HttpWebResponse res = (HttpWebResponse)request.GetResponse())
  164. {
  165. objCookieContainer = request.CookieContainer;
  166. //QueryRecordForm.LoginCookie = objCookieContainer.GetCookies(new Uri(strUrl));
  167. res.Cookies = objCookieContainer.GetCookies(new Uri(strUrl));
  168. foreach (Cookie c in res.Cookies)
  169. {
  170. }
  171. using (Stream resStream = res.GetResponseStream())
  172. {
  173. byte[] buffer = new byte[1024];
  174. int read;
  175. while ((read = resStream.Read(buffer, 0, buffer.Length)) > 0)
  176. {
  177. strResponse += Encoding.UTF8.GetString(buffer, 0, read);
  178. }
  179. }
  180. res.Close();
  181. }
  182. return strResponse;
  183. }
  184. catch (Exception e)
  185. {
  186. }
  187. return null;
  188. }
  189. public static string sendMessageCookie(string strUrl, string strPost, CookieContainer cookieContainer)
  190. {
  191. try
  192. {
  193. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
  194. if (cookieContainer != null)
  195. {
  196. request.CookieContainer = cookieContainer;
  197. }
  198. request.Method = "Post";
  199. request.Accept = "*/*";
  200. request.Headers.Add("Accept-Language: zh-CN,zh;q=0.8");
  201. request.Headers.Add("Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3");
  202. request.Headers.Add("Cache-Control: max-age=0");
  203. request.Accept = "text/xml,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
  204. request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36";
  205. request.Timeout = 10000;
  206. request.Referer = strUrl;//.Remove(strUrl.LastIndexOf("/"));
  207. if (!string.IsNullOrEmpty(strPost))
  208. {
  209. request.ContentType = "application/json; text/html; charset=UTF-8";
  210. byte[] byteData = Encoding.UTF8.GetBytes(strPost.ToString().TrimEnd('&'));
  211. request.ContentLength = byteData.Length;
  212. using (Stream reqStream = request.GetRequestStream())
  213. {
  214. reqStream.Write(byteData, 0, byteData.Length);
  215. reqStream.Close();
  216. }
  217. }
  218. string strResponse = "";
  219. using (HttpWebResponse res = (HttpWebResponse)request.GetResponse())
  220. {
  221. if (cookieContainer != null)
  222. {
  223. cookieContainer = request.CookieContainer;
  224. }
  225. using (Stream resStream = res.GetResponseStream())
  226. {
  227. byte[] buffer = new byte[1024];
  228. int read;
  229. while ((read = resStream.Read(buffer, 0, buffer.Length)) > 0)
  230. {
  231. strResponse += Encoding.UTF8.GetString(buffer, 0, read);
  232. }
  233. }
  234. res.Close();
  235. }
  236. return strResponse;
  237. }
  238. catch (Exception e)
  239. {
  240. //Console.WriteLine(e.ToString());
  241. }
  242. return null;
  243. }
  244. }
  245. class PostDateClass
  246. {
  247. String prop;
  248. public String Prop
  249. {
  250. get { return prop; }
  251. set { prop = value; }
  252. }
  253. String value;
  254. public String Value
  255. {
  256. get { return this.value; }
  257. set { this.value = value; }
  258. }
  259. /// <summary>
  260. /// 0为字符串,1为文件
  261. /// </summary>
  262. int type;
  263. public int Type
  264. {
  265. get { return type; }
  266. set { type = value; }
  267. }
  268. public PostDateClass(String prop, String value, int type = 0)
  269. {
  270. this.prop = prop;
  271. this.value = value;
  272. this.type = type;
  273. }
  274. }
  275. }