之前开发手持设备(RF)在WINCE环境用EVC开发,在客户端在EVC用C开发客户端界面,通过socket通信调用
服务端,服务端中间件调用后台数据库存储过程,基本业务逻辑在存储过程中实现。
最近在.net环境通过http请求实现新一版的程序开发。服务端可以是.net,也可以是java的服务程序,业务
逻辑可以在程序中实现,同样也可以放在存储过程中实现。我这里服务端用的.net MVC框架来实现的。
客户端具体的代码如下。
HttpUtil 类
1 using System; 2 using System.IO; 3 using System.Collections.Generic; 4 using System.Net; 5 6 namespace WMS 7 { 8 public sealed class HttpUtil 9 { 10 public int Timeout { get; set; } 11 12 13 public HttpUtil() 14 : this(100000) 15 { 16 17 } 18 19 public HttpUtil(int timeOut) 20 { 21 Timeout = timeOut; 22 } 23 24 public static T Post<T>(string reqUrl, object objParam) 25 { 26 HttpUtil httpClt = new HttpUtil(100000); 27 28 string responseContent = httpClt.Post(reqUrl, objParam); 29 30 if (!string.IsNullOrEmpty(responseContent)) 31 return Json.Converter.Deserialize<T>(responseContent); 32 return default(T); 33 } 34 35 private string GetPostContent(object o) 36 { 37 if (o == null) 38 return null; 39 40 var parameters = new List<string>(); 41 var t = o.GetType(); 42 foreach (var pi in t.GetProperties()) 43 { 44 var k = pi.Name; 45 if (pi.PropertyType.Name == "Int32[]") 46 { 47 var v = pi.GetValue(o, null); 48 if (v != null) 49 { 50 int[] ids = (int[])v; 51 for (int i = 0; i < ids.Length; i++) 52 parameters.Add(string.Format("{0}={1}", k + "[" + i + "]", ids[i])); 53 } 54 } 55 else 56 { 57 var v = pi.GetValue(o, null); 58 if (v != null) 59 parameters.Add(string.Format("{0}={1}", k, v)); 60 } 61 } 62 63 return string.Join("&", parameters.ToArray()); 64 } 65 66 private HttpWebRequest GetRequest(string url, string method, string postContent, string contentType) 67 { 68 HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest; 69 70 req.AllowAutoRedirect = false; 71 req.Method = method; 72 req.ContentType = contentType; 73 req.Timeout = Timeout; 74 req.SendChunked = true; 75 76 return req; 77 } 78 79 80 public string Get(string url) 81 { 82 return Request(url, "GET", null); 83 } 84 85 public static T Get<T>(string url) 86 { 87 HttpUtil httpClt = new HttpUtil(10000); 88 string responseContent = httpClt.Get(url); 89 90 if (!string.IsNullOrEmpty(responseContent)) 91 return Json.Converter.Deserialize<T>(responseContent); 92 return default(T); 93 } 94 95 public string Post(string url, object objParam) 96 { 97 return Request(url, "POST", GetPostContent(objParam)); 98 } 99 100 public string Request(string url, string method, string postContent) 101 { 102 return Request(url, method, postContent, "application/x-www-form-urlencoded;charset=UTF-8"); 103 } 104 105 public string Request(string url, string method, string postContent, string contentType) 106 { 107 108 HttpWebRequest req = GetRequest(url, method, postContent, contentType); 109 110 //请求数据 111 if (string.Equals(method, "POST", StringComparison.InvariantCultureIgnoreCase)) 112 { 113 byte[] bytesToPost = System.Text.Encoding.UTF8.GetBytes(postContent); 114 req.ContentLength = bytesToPost.Length; 115 116 using (Stream s = req.GetRequestStream()) 117 { 118 s.Write(bytesToPost, 0, bytesToPost.Length); 119 } 120 } 121 122 using (HttpWebResponse responseObj = req.GetResponse() as HttpWebResponse) 123 { 124 using (Stream s = responseObj.GetResponseStream()) 125 { 126 using (StreamReader sr = new StreamReader(s)) 127 { 128 return sr.ReadToEnd(); 129 } 130 } 131 } 132 133 } 134 } 135 }
login
1 using System; 2 using System.Drawing; 3 using System.Runtime.InteropServices; 4 using System.Windows.Forms; 5 6 namespace WMS 7 { 8 public partial class Login : Form 9 { 10 public Login() 11 { 12 InitializeComponent(); 13 txtName.Focus(); 14 } 15 16 private void txtName_KeyDown(object sender, KeyEventArgs e) 17 { 18 if (e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(txtName.Text)) 19 { 20 lblMessage.Text = ""; 21 txtPassword.Focus(); 22 txtPassword.SelectAll(); 23 } 24 } 25 26 private void txtPassword_KeyDown(object sender, KeyEventArgs e) 27 { 28 if (e.KeyCode == Keys.Enter && !string.IsNullOrEmpty(txtPassword.Text)) 29 { 30 Login(txtName.Text.Trim(), txtPassword.Text.Trim()); 31 } 32 } 33 34 private void btnLogin_Click(object sender, System.EventArgs e) 35 { 36 UserLogin(txtName.Text.Trim(), txtPassword.Text.Trim()); 37 } 38 39 private void Login(string name, string password) 40 { 41 btnLogin.Enabled = false; 42 txtPassword.Enabled = false; 43 44 lblMessage.Text = ""; 45 if (string.IsNullOrEmpty(name)) 46 { 47 lblMessage.Text = "用户名不能为空!"; 48 btnLogin.Enabled = true; 49 txtPassword.Enabled = true; 50 txtName.Focus(); 51 txtName.SelectAll(); 52 return; 53 } 54 if (string.IsNullOrEmpty(password)) 55 { 56 lblMessage.Text = "密码不能为空!"; 57 btnLogin.Enabled = true; 58 txtPassword.Enabled = true; 59 txtPassword.Focus(); 60 txtPassword.SelectAll(); 61 return; 62 } 63 try 64 { 65 HttpUtil.Post<ClientInfo>(url + "/Sys/Login", new 66 { 67 userName = name, 68 password = password 69 }); 70 if (ClientInfo.User_Id == 0) 71 { 72 lblMessage.Text = "用户名或密码错误!"; 73 btnLogin.Enabled = true; 74 txtPassword.Enabled = true; 75 txtName.Focus(); 76 txtName.SelectAll(); 77 } 78 else 79 { 80 Hide(); 81 Menu fmMenu = new Menu(); 82 fmMenu.ShowDialog(); 83 fmMenu.MaximizeBox = true; 84 } 85 } 86 catch (Exception ex) 87 { 88 lblMessage.Text = ex.Message; 89 btnLogin.Enabled = true; 90 txtPassword.Enabled = true; 91 txtName.Focus(); 92 txtName.SelectAll(); 93 } 94 } 95 96 97 98 private void btnLogOut_Click(object sender, EventArgs e) 99 { 100 Hide(); 101 LoginOut fm = new LoginOut(); 102 fm.Show(); 103 } 104 } 105 } 服务端实现类 1 #region 2 3 using System; 4 using System.Web.Mvc; 5 using WMS.Enum; 6 using Wms.Utility; 7 using Wms.Services; 8 using Wms.DomainModel; 9 #endregion 10 11 namespace Wms.Controllers 12 { 13 [HandleError] 14 public class SysController : Controller 15 { 16 17 18 [HttpPost] 19 public string Login(string userName, string password) 20 { 21 ISys_UserService service = UnityHelper.GetInstance<ISys_UserService>("Wms"); 22 string result = service.Login(userName, password); 23 return result; 24 } 25 } 26 }
如需要全部实现代码请联系 qq 1153755352