• C#post http传输数据


    服务端

     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);
            }
    

      

  • 相关阅读:
    图论-桥/割点/双连通分量/缩点/LCA
    未解决的问题
    hdu2586(LCA最近公共祖先)
    LCA最近公共祖先(least common ancestors)
    111
    poj1703 Find them, Catch them 并查集
    关于背包的注意事项
    codeforces343A A. Rational Resistance
    hdu(1171)多重背包
    HTML5事件—visibilitychange 页面可见性改变事件
  • 原文地址:https://www.cnblogs.com/lushixiong/p/6697805.html
Copyright © 2020-2023  润新知