HttpHandler.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Text;
  6. using System.Web;
  7. namespace UAS_LabelMachine.PublicMethod
  8. {
  9. public class HttpHandler : IHttpHandler
  10. {
  11. /// <summary>
  12. /// 您将需要在网站的 Web.config 文件中配置此处理程序
  13. /// 并向 IIS 注册它,然后才能使用它。有关详细信息,
  14. /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
  15. /// </summary>
  16. public bool IsReusable
  17. {
  18. // 如果无法为其他请求重用托管处理程序,则返回 false。
  19. // 如果按请求保留某些状态信息,则通常这将为 false。
  20. get { return true; }
  21. }
  22. public void ProcessRequest(HttpContext context)
  23. {
  24. }
  25. public static bool CheckUserLogin(string UserName, string PassWord, string Master, out string oMsg)
  26. {
  27. oMsg = "";
  28. try
  29. {
  30. string url = DataHelper.ERPAddesss + "mobile/login.action";//html调用的地址
  31. HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
  32. if (webrequest == null)
  33. {
  34. return false;
  35. }
  36. webrequest.Method = "POST";
  37. webrequest.Timeout = 1000;
  38. webrequest.ContentType = "application/x-www-form-urlencoded";
  39. System.Collections.Hashtable pars = new System.Collections.Hashtable();
  40. pars.Add("username", UserName);
  41. pars.Add("password", PassWord);
  42. pars.Add("master", Master);
  43. string buffer = "";
  44. //发送POST数据
  45. if (!(pars == null || pars.Count == 0))
  46. {
  47. foreach (string key in pars.Keys)
  48. {
  49. buffer = buffer + "&" + key + "=" + pars[key].ToString();
  50. }
  51. byte[] data = Encoding.UTF8.GetBytes(buffer);
  52. using (Stream stream = webrequest.GetRequestStream())
  53. {
  54. stream.Write(data, 0, data.Length);
  55. }
  56. }
  57. string[] values = webrequest.Headers.GetValues("Content-Type");
  58. WebResponse myResponse = webrequest.GetResponse();
  59. using (Stream resStream = myResponse.GetResponseStream())//得到回写的流
  60. {
  61. StreamReader newReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  62. string Content = newReader.ReadToEnd();
  63. Dictionary<string, object> dic = new Dictionary<string, object>();
  64. dic = BaseUtil.ToDictionary(Content);
  65. if (!dic.ContainsKey("erpaccount"))
  66. {
  67. oMsg = dic["reason"].ToString();
  68. return false;
  69. }
  70. newReader.Close();
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. LogManager.DoLog(ex.Message.ToString());
  76. }
  77. return true;
  78. }
  79. }
  80. }