HttpHelper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. namespace EMISOnline.WinService.Common
  8. {
  9. public class HttpHelper
  10. {
  11. public static string GetUrltoText(string Url, string encode)
  12. {
  13. try
  14. {
  15. System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
  16. // Get the response instance.
  17. System.Net.WebResponse wResp = wReq.GetResponse();
  18. System.IO.Stream respStream = wResp.GetResponseStream();
  19. // Dim reader As StreamReader = New StreamReader(respStream)
  20. using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding(encode)))
  21. {
  22. return reader.ReadToEnd();
  23. }
  24. }
  25. catch (System.Exception ex)
  26. {
  27. //errorMsg = ex.Message;
  28. }
  29. return "";
  30. }
  31. public static string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
  32. {
  33. string ret = string.Empty;
  34. try
  35. {
  36. byte[] byteArray = dataEncode.GetBytes(paramData); //转化
  37. HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
  38. webReq.Method = "POST";
  39. webReq.ContentType = "application/x-www-form-urlencoded";
  40. webReq.ContentLength = byteArray.Length;
  41. Stream newStream = webReq.GetRequestStream();
  42. newStream.Write(byteArray, 0, byteArray.Length);//写入参数
  43. newStream.Close();
  44. HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
  45. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
  46. ret = sr.ReadToEnd();
  47. sr.Close();
  48. response.Close();
  49. newStream.Close();
  50. }
  51. catch (Exception ex)
  52. {
  53. }
  54. return ret;
  55. }
  56. }
  57. }