PageRenderTime 36ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/encog-core-silverlight/encog-core-silverlight/Bot/BotUtil.cs

http://encog-cs.googlecode.com/
C# | 323 lines | 184 code | 48 blank | 91 comment | 25 complexity | 56e2301b46f45e96fbc1c07e2908fcda MD5 | raw file
  1. // Encog(tm) Artificial Intelligence Framework v2.5
  2. // .Net Version
  3. // http://www.heatonresearch.com/encog/
  4. // http://code.google.com/p/encog-java/
  5. //
  6. // Copyright 2008-2010 by Heaton Research Inc.
  7. //
  8. // Released under the LGPL.
  9. //
  10. // This is free software; you can redistribute it and/or modify it
  11. // under the terms of the GNU Lesser General Public License as
  12. // published by the Free Software Foundation; either version 2.1 of
  13. // the License, or (at your option) any later version.
  14. //
  15. // This software is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. // Lesser General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU Lesser General Public
  21. // License along with this software; if not, write to the Free
  22. // Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. // 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  24. //
  25. // Encog and Heaton Research are Trademarks of Heaton Research, Inc.
  26. // For information on Heaton Research trademarks, visit:
  27. //
  28. // http://www.heatonresearch.com/copyright.html
  29. #if !SILVERLIGHT
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Text;
  34. using System.IO;
  35. using System.Net;
  36. using Encog.Util.HTTP;
  37. using log4net;
  38. using Encog.Util;
  39. namespace Encog.Bot
  40. {
  41. /// <summary>
  42. /// Utility class for bots.
  43. /// </summary>
  44. public class BotUtil
  45. {
  46. /// <summary>
  47. /// How much data to read at once.
  48. /// </summary>
  49. public static int BUFFER_SIZE = 8192;
  50. #if logging
  51. /// <summary>
  52. /// The logging object.
  53. /// </summary>
  54. private static readonly ILog LOGGER = LogManager.GetLogger(typeof(BotUtil));
  55. #endif
  56. /// <summary>
  57. /// This method is very useful for grabbing information from a HTML page.
  58. /// </summary>
  59. /// <param name="str">The string to search.</param>
  60. /// <param name="token1">The text, or tag, that comes before the desired text</param>
  61. /// <param name="token2">The text, or tag, that comes after the desired text</param>
  62. /// <param name="index">Index in the string to start searching from.</param>
  63. /// <param name="occurence">What occurence.</param>
  64. /// <returns>The contents of the URL that was downloaded.</returns>
  65. public static String ExtractFromIndex(String str, String token1,
  66. String token2, int index, int occurence)
  67. {
  68. int location1, location2;
  69. // convert everything to lower case
  70. String searchStr = str.ToLower();
  71. String token1Lower = token1.ToLower();
  72. String token2Lower = token2.ToLower();
  73. int count = occurence;
  74. // now search
  75. location1 = location2 = index - 1;
  76. do
  77. {
  78. location1 = searchStr.IndexOf(token1Lower, location1 + 1);
  79. if (location1 == -1)
  80. {
  81. return null;
  82. }
  83. count--;
  84. } while (count > 0);
  85. // return the result from the original string that has mixed
  86. // case
  87. location2 = searchStr.IndexOf(token2Lower, location1 + 1);
  88. if (location2 == -1)
  89. {
  90. return null;
  91. }
  92. return str.Substring(location1 + token1Lower.Length, location2 - (location1 + token1.Length));
  93. }
  94. /// <summary>
  95. /// This method is very useful for grabbing information from a HTML page.
  96. /// </summary>
  97. /// <param name="str">The string to search.</param>
  98. /// <param name="token1">The text, or tag, that comes before the desired text.</param>
  99. /// <param name="token2">The text, or tag, that comes after the desired text.</param>
  100. /// <param name="index">Which occurrence of token1 to use, 1 for the first.</param>
  101. /// <returns>The contents of the URL that was downloaded.</returns>
  102. public static String Extract(String str, String token1,
  103. String token2, int index)
  104. {
  105. int location1, location2;
  106. // convert everything to lower case
  107. String searchStr = str.ToLower();
  108. String token1Lower = token1.ToLower();
  109. String token2Lower = token2.ToLower();
  110. int count = index;
  111. // now search
  112. location1 = location2 = -1;
  113. do
  114. {
  115. location1 = searchStr.IndexOf(token1Lower, location1 + 1);
  116. if (location1 == -1)
  117. {
  118. return null;
  119. }
  120. count--;
  121. } while (count > 0);
  122. // return the result from the original string that has mixed
  123. // case
  124. location2 = searchStr.IndexOf(token2Lower, location1 + 1);
  125. if (location2 == -1)
  126. {
  127. return null;
  128. }
  129. return str.Substring(location1 + token1Lower.Length, location2 - (location1 + token1.Length));
  130. }
  131. /// <summary>
  132. /// Post to a page.
  133. /// </summary>
  134. /// <param name="uri">The URI to post to.</param>
  135. /// <param name="param">The post params.</param>
  136. /// <returns>The HTTP response.</returns>
  137. public static String POSTPage(Uri uri, IDictionary<String, String> param)
  138. {
  139. WebRequest req = WebRequest.Create(uri);
  140. req.ContentType = "application/x-www-form-urlencoded";
  141. req.Method = "POST";
  142. StringBuilder postString = new StringBuilder();
  143. foreach (String key in param.Keys)
  144. {
  145. String value = param[key];
  146. if (value != null)
  147. {
  148. if (postString.Length != 0)
  149. postString.Append('&');
  150. postString.Append(key);
  151. postString.Append('=');
  152. postString.Append( FormUtility.Encode(value));
  153. }
  154. }
  155. byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postString.ToString());
  156. req.ContentLength = bytes.Length;
  157. System.IO.Stream os = req.GetRequestStream();
  158. os.Write(bytes, 0, bytes.Length);
  159. os.Close();
  160. System.Net.WebResponse resp = req.GetResponse();
  161. if (resp == null) return null;
  162. System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
  163. return sr.ReadToEnd().Trim();
  164. }
  165. /// <summary>
  166. /// Post bytes to a page.
  167. /// </summary>
  168. /// <param name="uri">The URI to post to.</param>
  169. /// <param name="bytes">The bytes to post.</param>
  170. /// <param name="length">The length of the posted data.</param>
  171. /// <returns>The HTTP response.</returns>
  172. public static String POSTPage(Uri uri, byte[] bytes, int length)
  173. {
  174. WebRequest webRequest = WebRequest.Create(uri);
  175. //webRequest.ContentType = "application/x-www-form-urlencoded";
  176. webRequest.Method = "POST";
  177. webRequest.ContentLength = length;
  178. Stream os = null;
  179. try
  180. { // send the Post
  181. webRequest.ContentLength = bytes.Length; //Count bytes to send
  182. os = webRequest.GetRequestStream();
  183. os.Write(bytes, 0, length); //Send it
  184. }
  185. catch (WebException ex)
  186. {
  187. throw new BotError(ex);
  188. }
  189. finally
  190. {
  191. if (os != null)
  192. {
  193. os.Flush();
  194. }
  195. }
  196. try
  197. { // get the response
  198. WebResponse webResponse = webRequest.GetResponse();
  199. if (webResponse == null)
  200. {
  201. return null;
  202. }
  203. StreamReader sr = new StreamReader(webResponse.GetResponseStream());
  204. return sr.ReadToEnd();
  205. }
  206. catch (WebException ex)
  207. {
  208. throw new BotError(ex);
  209. }
  210. }
  211. /// <summary>
  212. /// Load the specified web page into a string.
  213. /// </summary>
  214. /// <param name="url">The url to load.</param>
  215. /// <returns>The web page as a string.</returns>
  216. public static String LoadPage(Uri url)
  217. {
  218. try
  219. {
  220. StringBuilder result = new StringBuilder();
  221. byte[] buffer = new byte[BotUtil.BUFFER_SIZE];
  222. int length;
  223. WebRequest http = HttpWebRequest.Create(url);
  224. HttpWebResponse response = (HttpWebResponse)http.GetResponse();
  225. Stream istream = response.GetResponseStream();
  226. do
  227. {
  228. length = istream.Read(buffer, 0, buffer.Length);
  229. if (length > 0)
  230. {
  231. String str = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
  232. result.Append(str);
  233. }
  234. } while (length > 0);
  235. return result.ToString();
  236. }
  237. catch (IOException e)
  238. {
  239. #if logging
  240. if (BotUtil.LOGGER.IsErrorEnabled)
  241. {
  242. BotUtil.LOGGER.Error("Exception", e);
  243. }
  244. #endif
  245. throw new BotError(e);
  246. }
  247. }
  248. /// <summary>
  249. /// Private constructor.
  250. /// </summary>
  251. private BotUtil()
  252. {
  253. }
  254. /// <summary>
  255. /// Post to a page.
  256. /// </summary>
  257. /// <param name="uri">The URI to post to.</param>
  258. /// <param name="stream">The stream.</param>
  259. /// <returns>The page returned.</returns>
  260. public static string POSTPage(Uri uri, Stream stream)
  261. {
  262. WebRequest req = WebRequest.Create(uri);
  263. //req.ContentType = "application/x-www-form-urlencoded";
  264. req.Method = "POST";
  265. System.IO.Stream os = req.GetRequestStream();
  266. FileUtil.CopyStream(stream, os);
  267. os.Close();
  268. System.Net.WebResponse resp = req.GetResponse();
  269. if (resp == null) return null;
  270. System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
  271. return sr.ReadToEnd().Trim();
  272. }
  273. }
  274. }
  275. #endif