ISearchEngine.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ProxyPattern
  9. {
  10. public interface ISearchEngine
  11. {
  12. void Search(string searchStr);
  13. }
  14. public class Google : ISearchEngine
  15. {
  16. public void Search(string searchStr)
  17. {
  18. string url = "https://www.google.com/search?q=";
  19. var request = (HttpWebRequest)WebRequest.Create(url + searchStr);
  20. var response = (HttpWebResponse)request.GetResponse();
  21. if (response != null)
  22. {
  23. Console.WriteLine(string.Format("搜索【{0}】并成功返回!", searchStr));
  24. }
  25. }
  26. }
  27. public class GoogleProxy : ISearchEngine
  28. {
  29. private readonly ISearchEngine _vpn = null;
  30. public GoogleProxy()
  31. {
  32. _vpn = new Google();
  33. }
  34. public void Search(string searchStr)
  35. {
  36. try
  37. {
  38. _vpn.Search(searchStr);
  39. }
  40. catch (Exception)
  41. {
  42. string url = "https://www.baidu.com/search?q=";
  43. var request = (HttpWebRequest)WebRequest.Create(url + searchStr);
  44. try
  45. {
  46. var response = (HttpWebResponse)request.GetResponse();
  47. if (response != null)
  48. {
  49. Console.WriteLine(string.Format("搜索【{0}】并成功返回!", searchStr));
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. Console.WriteLine(ex.Message);
  55. }
  56. }
  57. }
  58. }
  59. }