clsWebService.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. namespace HNWD.Pregrant.Common
  7. {
  8. public class clsWebService
  9. {
  10. private string webServerIP = string.Empty;
  11. private WebClient webClient = new WebClient();
  12. public clsWebService(string webServerIP)
  13. {
  14. this.webServerIP = webServerIP;
  15. webClient.Credentials = CredentialCache.DefaultCredentials;
  16. webClient.Encoding = System.Text.Encoding.GetEncoding("UTF-8");
  17. }
  18. public string WebServerIP
  19. {
  20. get { return this.webServerIP; }
  21. set { this.webServerIP = value; }
  22. }
  23. public string HttpServer
  24. {
  25. get { return "http://" + this.webServerIP + "/"; }
  26. }
  27. public string GetDataString(string subURL, string strPostData)
  28. {
  29. Byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(strPostData.ToString());
  30. webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  31. webClient.Headers.Add("ContentLength", strPostData.Length.ToString());
  32. return Encoding.UTF8.GetString(webClient.UploadData(this.HttpServer + subURL, "POST", bytes));
  33. }
  34. public string GetDataString(string subURL, NameValueCollection nameValueCollection)
  35. {
  36. webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
  37. return Encoding.UTF8.GetString(webClient.UploadValues(this.HttpServer + subURL, "POST", nameValueCollection));
  38. }
  39. public string GetDataString(string subURL)
  40. {
  41. return Encoding.UTF8.GetString(webClient.DownloadData(new System.Uri(this.HttpServer + subURL)));
  42. }
  43. public Boolean GetDataFile(string subURL, string FileName)
  44. {
  45. bool bRes = false;
  46. if (File.Exists(FileName))
  47. {
  48. File.Delete(FileName);
  49. }
  50. webClient.DownloadFile(this.HttpServer + subURL, FileName);
  51. if (File.Exists(FileName))
  52. {
  53. bRes = true;
  54. }
  55. return bRes;
  56. }
  57. }
  58. }