本文整理汇总了C#中System.Net.HttpListenerResponse.WriteJson方法的典型用法代码示例。如果您正苦于以下问题:C# HttpListenerResponse.WriteJson方法的具体用法?C# HttpListenerResponse.WriteJson怎么用?C# HttpListenerResponse.WriteJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.HttpListenerResponse
的用法示例。
在下文中一共展示了HttpListenerResponse.WriteJson方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnStart
private void OnStart(HttpListenerRequest req, HttpListenerResponse resp)
{
if (req.HttpMethod != "POST")
{
resp.Write405();
return;
}
if (!req.HasEntityBody)
{
resp.Write400();
return;
}
CnCStartRequest args;
using (var ist = new StreamReader(req.InputStream))
{
var body = ist.ReadToEnd();
args = body.FromJson<CnCStartRequest>();
}
if (args.job == Guid.Empty)
{
args.job = Guid.NewGuid();
}
if (args.job == Guid.Empty || jobs.ContainsKey(args.job))
{
resp.Write400();
return;
}
if (args.type == "http")
{
if (args.http == null)
{
resp.Write400();
return;
}
args.http.Tag = args.job;
var http = new HttpLoader(args.http);
jobs[args.job] = http;
lock (jobQueue)
{
jobQueue.Enqueue(http);
}
workerSignal.Set();
//speculatively wait a few seconds, in case the job is done real quick like
Thread.Sleep(2000);
resp.WriteJson(new
{
Location = "/status/" + args.job,
Data = new Result(http)
}, http.Status == Status.Complete ? 200 : 202);
}
else
{
resp.Write400();
return;
}
}
示例2: OnStatusSpecific
private void OnStatusSpecific(HttpListenerRequest req, HttpListenerResponse resp)
{
if (req.HttpMethod != "GET")
{
resp.Write405();
}
var g = req.Url.AbsolutePath.Substring(8);
Guid job;
if (!Guid.TryParse(g, out job) || !jobs.ContainsKey(job))
{
resp.Write404();
return;
}
resp.WriteJson(new Result(jobs[job]));
}
示例3: StatusGeneral
private void StatusGeneral(HttpListenerRequest req, HttpListenerResponse resp)
{
IList<Guid> queued, comp;
lock (jobQueue)
{
queued = jobQueue.Select(j => j.Tag).ToList();
}
lock (completed)
{
comp = completed.Select(c => c.Tag).ToList();
}
var cur = current;
resp.WriteJson(new
{
enqueued = queued,
current = cur != null ? (Guid?)cur.Tag : (Guid?)null,
completed = comp
});
}
示例4: OnGetSlave
private void OnGetSlave(HttpListenerRequest req, HttpListenerResponse res)
{
res.WriteJson(slaves);
}