|
|
@@ -23,22 +23,25 @@ namespace Leadercraft.Server |
|
|
|
public LeadercraftResult<KeyStruct> RequestPOSTToken(string playerName = "???") |
|
|
|
{ |
|
|
|
NewKeyStruct reqBodyObj = new NewKeyStruct{ PlayerID = _userId, PlayerName = playerName }; |
|
|
|
byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(reqBodyObj)); |
|
|
|
// Request |
|
|
|
string reqBodyStr = JsonConvert.SerializeObject(reqBodyObj); |
|
|
|
// Request |
|
|
|
WebRequest request = WebRequest.Create(_tokenUrl); |
|
|
|
request.Method = "POST"; |
|
|
|
request.ContentLength = reqBodyBytes.Length; |
|
|
|
request.ContentLength = reqBodyStr.Length; |
|
|
|
request.ContentType = "application/json"; |
|
|
|
Stream body; |
|
|
|
StreamWriter body; |
|
|
|
try |
|
|
|
{ |
|
|
|
body = request.GetRequestStream(); |
|
|
|
body.Write(reqBodyBytes, 0, reqBodyBytes.Length); |
|
|
|
body = new StreamWriter(request.GetRequestStream()); |
|
|
|
body.Write(reqBodyStr); |
|
|
|
body.Close(); |
|
|
|
} |
|
|
|
catch (WebException e) |
|
|
|
{ |
|
|
|
return new LeadercraftResult<KeyStruct>(new byte[] { }, (int)e.Status); |
|
|
|
int status = (int) e.Status; |
|
|
|
if (e.Response != null && e.Response is HttpWebResponse webResponse) |
|
|
|
status = (int)webResponse.StatusCode; |
|
|
|
return new LeadercraftResult<KeyStruct>(new byte[] { }, status); |
|
|
|
} |
|
|
|
// Response |
|
|
|
HttpWebResponse response = null; |
|
|
@@ -48,35 +51,40 @@ namespace Leadercraft.Server |
|
|
|
} |
|
|
|
catch (WebException e) |
|
|
|
{ |
|
|
|
return new LeadercraftResult<KeyStruct>(new byte[] { }, (int)e.Status); |
|
|
|
int status = (int) e.Status; |
|
|
|
if (e.Response != null && e.Response is HttpWebResponse webResponse) |
|
|
|
status = (int)webResponse.StatusCode; |
|
|
|
return new LeadercraftResult<KeyStruct>(new byte[] { }, status); |
|
|
|
} |
|
|
|
body = response.GetResponseStream(); |
|
|
|
byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))]; |
|
|
|
body.Read(respBodyBytes, 0, respBodyBytes.Length); |
|
|
|
StreamReader respBody = new StreamReader(response.GetResponseStream()); |
|
|
|
string respBodyStr = respBody.ReadToEnd(); |
|
|
|
response.Close(); |
|
|
|
return new LeadercraftResult<KeyStruct>(respBodyBytes, (int)response.StatusCode); |
|
|
|
return new LeadercraftResult<KeyStruct>(respBodyStr, (int)response.StatusCode); |
|
|
|
} |
|
|
|
|
|
|
|
public LeadercraftResult<string> RequestPOSTCriteria(CriteriaStruct criteria, string token) |
|
|
|
{ |
|
|
|
criteria.PlayerID = _userId; |
|
|
|
byte[] reqBodyBytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(criteria)); |
|
|
|
string reqBodyStr = JsonConvert.SerializeObject(criteria); |
|
|
|
// Request |
|
|
|
WebRequest request = WebRequest.Create(_criteriaUrl); |
|
|
|
request.Method = "POST"; |
|
|
|
request.ContentLength = reqBodyBytes.Length; |
|
|
|
request.ContentLength = reqBodyStr.Length; |
|
|
|
request.ContentType = "application/json"; |
|
|
|
request.Headers.Add(HttpRequestHeader.Authorization, "leadercraft "+token); |
|
|
|
Stream body; |
|
|
|
StreamWriter body; |
|
|
|
try |
|
|
|
{ |
|
|
|
body = request.GetRequestStream(); |
|
|
|
body.Write(reqBodyBytes, 0, reqBodyBytes.Length); |
|
|
|
body = new StreamWriter(request.GetRequestStream()); |
|
|
|
body.Write(reqBodyStr); |
|
|
|
body.Close(); |
|
|
|
} |
|
|
|
catch (WebException e) |
|
|
|
{ |
|
|
|
return new LeadercraftResult<string>(new byte[] { }, (int)e.Status); |
|
|
|
int status = (int) e.Status; |
|
|
|
if (e.Response != null && e.Response is HttpWebResponse webResponse) |
|
|
|
status = (int)webResponse.StatusCode; |
|
|
|
return new LeadercraftResult<string>(new byte[] { }, status); |
|
|
|
} |
|
|
|
// Response |
|
|
|
HttpWebResponse response = null; |
|
|
@@ -86,13 +94,15 @@ namespace Leadercraft.Server |
|
|
|
} |
|
|
|
catch (WebException e) |
|
|
|
{ |
|
|
|
return new LeadercraftResult<string>(new byte[] { }, (int)e.Status); |
|
|
|
int status = (int) e.Status; |
|
|
|
if (e.Response != null && e.Response is HttpWebResponse webResponse) |
|
|
|
status = (int)webResponse.StatusCode; |
|
|
|
return new LeadercraftResult<string>(new byte[] { }, status); |
|
|
|
} |
|
|
|
body = response.GetResponseStream(); |
|
|
|
byte[] respBodyBytes = new byte[int.Parse(response.GetResponseHeader("Content-Length"))]; |
|
|
|
body.Read(respBodyBytes, 0, respBodyBytes.Length); |
|
|
|
StreamReader respBody = new StreamReader(response.GetResponseStream()); |
|
|
|
string respBodyStr = respBody.ReadToEnd(); |
|
|
|
response.Close(); |
|
|
|
return new LeadercraftResult<string>(respBodyBytes, (int)response.StatusCode); |
|
|
|
return new LeadercraftResult<string>(respBodyStr, (int)response.StatusCode); |
|
|
|
} |
|
|
|
} |
|
|
|
} |