发送json流到服务器
本文关键字:服务器 json 发送 | 更新日期: 2023-09-27 18:13:37
我在发送json字符串到asp.net页面的问题,我想发送长流b刚才我正在尝试短一个。我不知道这条路对不对,但我也不知道另外一条路。它不是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");
}
}
使用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参数的方式。
在url上放置字符串时也使用Server.URLEncode
:
onlineApp = settings.onlinURL
+ @"/Admin/HR/Attendance_UpdateData.ashx?" + Server.URLEncode(queryString);