Added saving http response content as a byte array to a file. (#124)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -96,7 +97,7 @@ namespace Barotrauma
|
|||||||
HandleNetMessage(netMessage, name, client);
|
HandleNetMessage(netMessage, name, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void HttpRequest(string url, LuaCsAction callback, string data = null, string method = "POST", string contentType = "application/json", Dictionary<string, string> headers = null)
|
public async void HttpRequest(string url, LuaCsAction callback, string data = null, string method = "POST", string contentType = "application/json", Dictionary<string, string> headers = null, string savePath = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -114,9 +115,22 @@ namespace Barotrauma
|
|||||||
{
|
{
|
||||||
request.Content = new StringContent(data, Encoding.UTF8, contentType);
|
request.Content = new StringContent(data, Encoding.UTF8, contentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpResponseMessage response = await client.SendAsync(request);
|
HttpResponseMessage response = await client.SendAsync(request);
|
||||||
|
|
||||||
|
if (savePath != null)
|
||||||
|
{
|
||||||
|
if (LuaCsFile.IsPathAllowedException(savePath))
|
||||||
|
{
|
||||||
|
byte[] responseData = await response.Content.ReadAsByteArrayAsync();
|
||||||
|
|
||||||
|
using (var fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write))
|
||||||
|
{
|
||||||
|
fileStream.Write(responseData, 0, responseData.Length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
string responseBody = await response.Content.ReadAsStringAsync();
|
string responseBody = await response.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
GameMain.LuaCs.Timer.Wait((object[] par) =>
|
GameMain.LuaCs.Timer.Wait((object[] par) =>
|
||||||
@@ -134,15 +148,15 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void HttpPost(string url, LuaCsAction callback, string data, string contentType = "application/json", Dictionary<string, string> headers = null)
|
public void HttpPost(string url, LuaCsAction callback, string data, string contentType = "application/json", Dictionary<string, string> headers = null, string savePath = null)
|
||||||
{
|
{
|
||||||
HttpRequest(url, callback, data, "POST", contentType, headers);
|
HttpRequest(url, callback, data, "POST", contentType, headers, savePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void HttpGet(string url, LuaCsAction callback, Dictionary<string, string> headers = null)
|
public void HttpGet(string url, LuaCsAction callback, Dictionary<string, string> headers = null, string savePath = null)
|
||||||
{
|
{
|
||||||
HttpRequest(url, callback, null, "GET", null, headers);
|
HttpRequest(url, callback, null, "GET", null, headers, savePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateEntityEvent(INetSerializable entity, NetEntityEvent.IData extraData)
|
public void CreateEntityEvent(INetSerializable entity, NetEntityEvent.IData extraData)
|
||||||
|
|||||||
Reference in New Issue
Block a user