将json流发送到服务器

时间:2014-07-20 06:27:24

标签: c# asp.net json

我在将json字符串发送到asp.net页面时遇到问题,我想发送长流bs,我正在尝试使用短流。我不知道这是不是正确的方法,但我不知道另一个。  它没有以json格式到达! 这在c#:

中的客户端代码中
List<Employee> eList = new List<Employee>();
                                    Employee eo = new Employee();
                                    eo = new Employee();
                                    eo.Name = "Santosh";
                                    eo.Age = 24;
                                    eList.Add(eo);

                                    queryString = JsonConvert.SerializeObject(eList, Newtonsoft.Json.Formatting.Indented);

                                    if (flagFirstTime==0)
                                    {
                                        onlineApp = settings.onlinURL + @"/Admin/HR/Attendance_UpdateData.ashx?" + queryString;//here the json string 
                                        urlAdress = onlineApp;
                                        flagFirstTime = 1;
                                    }


                                    Uri url = new Uri(urlAdress);
                                    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(url);
                                    webReq.Method = "Get";

并在asp.net页面中:

public void ProcessRequest(HttpContext context) 
    { 
        Teachers_Att_Data teacherAttDataNew; 

        try 
        { 

            var jsonString = String.Empty; 

            var stream = context.Request.InputStream; 
            byte[] buffer = new byte[stream.Length]; 
            stream.Read(buffer, 0, buffer.Length); 
            string xml = Encoding.UTF8.GetString(buffer); 


            context.Response.ContentType = "text/plain"; 
            context.Response.Write("updated"); 

        }  
        catch (Exception ex) 
        { 
                  context.Response.Write("f"); 
        } 


    }

1 个答案:

答案 0 :(得分:0)

使用Request.Url.Query在您发送字符串时读取字符串,就像我看到的那样。所以你的处理程序必须像:

public void ProcessRequest(HttpContext context) 
    { 
        Teachers_Att_Data teacherAttDataNew; 

        try 
        { 
            string xml = context.Request.Url.Query.ToString();

            context.Response.ContentType = "text/plain"; 
            context.Response.Write("updated");     
        }  
        catch (Exception ex) 
        { 
                  context.Response.Write("f"); 
        } 
    }

请注意,我不确定其余部分是否有效,或者您的程序逻辑是什么,但这是阅读网址参数的方法。

将字符串放在网址上时也使用Server.URLEncode

onlineApp = settings.onlinURL 
     + @"/Admin/HR/Attendance_UpdateData.ashx?" + Server.URLEncode(queryString);