2014-07-20 84 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);