服务端
System.IO.Stream s = context.Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder builder = new StringBuilder(); while ((count = s.Read(buffer, 0, 1024)) > 0) { builder.Append(Encoding.UTF8.GetString(buffer, 0, count)); } s.Flush(); s.Close(); s.Dispose(); UserInfo user = new UserInfo(); JavaScriptSerializer serializer = new JavaScriptSerializer(); //var json = serializer.Serialize(p); ; var p1 = serializer.Deserialize<UserInfo>(builder.ToString()); SqlConnection conn3 = new SqlConnection("Data Source = .; Initial Catalog = 用户中心; User Id = sa; Password = 666666"); conn3.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO [用户中心].[dbo].[UserInfo] ([UserName], [Pwd], [State], [QQ], [Mobile], [Email], [CreateTime], [RegIp], [ClientId], [Money], [SecurityQuestion], [Answer], [VerfyMobile], [VerfyEmail], [SouceUserId]) VALUES ( N'8', N'8', NULL, N'8', N'8', N'8', N'04 10 2017 3:42PM', NULL, NULL, N'0', NULL, NULL, '0', '0', '0')", conn3); cmd.ExecuteNonQuery(); SqlCommand comm = new SqlCommand("select * from UserInfo where UserName='8'", conn3); SqlDataReader dataReader = comm.ExecuteReader(); string dd = ""; while (dataReader.Read()) { dd = dataReader[0].ToString(); } context.Response.Write(dd.ToString());
客户端
public ActionResult PostId() { UserInfo user = new UserInfo(); user.UserName = "张三"; JavaScriptSerializer serializer = new JavaScriptSerializer(); var json = serializer.Serialize(user); string JSONData =json.ToString(); string Url= "http://localhost:11381/index.ashx"; byte[] bytes = Encoding.UTF8.GetBytes(JSONData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentLength = bytes.Length; request.ContentType = "text/xml"; Stream reqstream = request.GetRequestStream(); reqstream.Write(bytes, 0, bytes.Length); //声明一个HttpWebRequest请求 request.Timeout = 90000; //设置连接超时时间 request.Headers.Set("Pragma", "no-cache"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream streamReceive = response.GetResponseStream(); Encoding encoding = Encoding.UTF8; StreamReader streamReader = new StreamReader(streamReceive, encoding); string strResult = streamReader.ReadToEnd(); streamReceive.Dispose(); streamReader.Dispose(); return Content(strResult); }