Push updated code
This commit is contained in:
parent
d1ce65cce7
commit
62c4eb5850
9 changed files with 549 additions and 2 deletions
89
HTTP.cs
Normal file
89
HTTP.cs
Normal file
|
@ -0,0 +1,89 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
namespace LibZNI
|
||||
{
|
||||
public class HTTP
|
||||
{
|
||||
public static HTTPReplyData performRequest(string url, string sJson)
|
||||
{
|
||||
HttpRequestMessage hrm = new HttpRequestMessage();
|
||||
hrm.Method = HttpMethod.Post;
|
||||
hrm.RequestUri = new Uri(url);
|
||||
hrm.Content = new StringContent(sJson, Encoding.UTF8, "application/json");
|
||||
return HTTP.Request(hrm);
|
||||
}
|
||||
public static HTTPReplyData performRequest(string url, string sJson, string xSLOwner)
|
||||
{
|
||||
HttpRequestMessage hrm = new HttpRequestMessage();
|
||||
hrm.Method = HttpMethod.Post;
|
||||
hrm.RequestUri = new Uri(url);
|
||||
hrm.Headers.Add("X-SecondLife-Owner-Key", xSLOwner);
|
||||
hrm.Content = new StringContent(sJson, Encoding.UTF8, "application/json");
|
||||
return HTTP.Request(hrm);
|
||||
}
|
||||
public static HTTPReplyData Request(HttpRequestMessage request)
|
||||
{
|
||||
HTTPReplyData rd = new HTTPReplyData();
|
||||
HttpClient client = new HttpClient();
|
||||
Task<HttpResponseMessage> t_hrm = client.SendAsync(request);
|
||||
t_hrm.Wait();
|
||||
|
||||
HttpResponseMessage response = t_hrm.Result;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
rd.Code = (int)response.StatusCode;
|
||||
HttpContent cnt = response.Content;
|
||||
HttpResponseHeaders hrh = response.Headers;
|
||||
|
||||
foreach (KeyValuePair<string, IEnumerable<string>> kvp in hrh)
|
||||
{
|
||||
foreach(string key in kvp.Value)
|
||||
{
|
||||
rd.Headers[kvp.Key] = key;
|
||||
}
|
||||
}
|
||||
HttpContentHeaders hch = cnt.Headers;
|
||||
|
||||
foreach (KeyValuePair<string, IEnumerable<string>> kvp in hch)
|
||||
{
|
||||
foreach (string key in kvp.Value)
|
||||
{
|
||||
rd.Headers[kvp.Key] = key;
|
||||
}
|
||||
}
|
||||
rd.ContentType = rd.Headers["Content-Type"];
|
||||
Task<byte[]> bf = cnt.ReadAsByteArrayAsync();
|
||||
bf.Wait();
|
||||
rd.MessageAsBytes = bf.Result;
|
||||
|
||||
rd.MessageAsString = Encoding.UTF8.GetString(bf.Result);
|
||||
|
||||
|
||||
|
||||
return rd;
|
||||
}catch(Exception ex)
|
||||
{
|
||||
return rd;
|
||||
}
|
||||
}
|
||||
}
|
||||
public class HTTPReplyData
|
||||
{
|
||||
public int Code;
|
||||
public string ContentType;
|
||||
public string MessageAsString;
|
||||
public byte[] MessageAsBytes;
|
||||
public Dictionary<string, string> Headers=new Dictionary<string, string>();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue