forked from ln-dotnet/ln.json
55 lines
1.2 KiB
C#
55 lines
1.2 KiB
C#
using System;
|
|
using sharp.extensions;
|
|
using System.Net;
|
|
using sharp.webclient;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
|
|
namespace sharp.json
|
|
{
|
|
public class JSONWebRequest
|
|
{
|
|
private static JSONParser jsonParser = new JSONParser();
|
|
|
|
public static JSON Call(string url){
|
|
return Call(url, null);
|
|
}
|
|
|
|
public static JSON Call(string url, JSON request)
|
|
{
|
|
return Call(url, request, null);
|
|
}
|
|
|
|
public static JSON Call(string url, JSON request, IDictionary<string,string> headers)
|
|
{
|
|
HttpRequest req = new HttpRequest(url);
|
|
if (request != null){
|
|
byte[] rbody = request.ToString().toBytes();
|
|
req.RequestStream.Write(rbody,0,rbody.Length);
|
|
}
|
|
|
|
if (headers != null)
|
|
{
|
|
foreach (string hname in headers.Keys)
|
|
{
|
|
req.setHeader(hname, headers[hname]);
|
|
}
|
|
}
|
|
|
|
HttpResponse response = req.Send();
|
|
|
|
try {
|
|
return jsonParser.Parse(response.ContentText);
|
|
} catch (Exception e){
|
|
Console.WriteLine("JSONWebRequest could not parse response.");
|
|
Console.WriteLine("Response was:");
|
|
Console.WriteLine(">-------------------------------------------");
|
|
Console.WriteLine(response.ContentText);
|
|
Console.WriteLine("<-------------------------------------------");
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|