2014-07-20 147 views
0

我有发送json字符串到asp.net页面的问题,我想发送很长的流bs,现在我正在尝试短一个。我不知道这是否正确,但我不知道另一个。 它不会以json格式到达! 这在C#中的客户端代码:发送json流到服务器

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


    } 
+0

发送您的JSON作为查询字符串几乎必然会导致长远来看,你长的问题:

当您将字符串URL上作为同样使用Server.URLEncode。将其设置为您的请求主体。此外,使用“StreamReader”来读取正文,而不是模拟自己的方式。 '新的StreamReader(context.Request.InputStream).ReadToEnd();' –

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

请注意,我不知道,如果剩下的工作,或者是你的逻辑是你的程序,但是这是读取URL参数的方式。

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