Here is my web request code block,
scroll down to the bottom of it to the catch exception
try
{
var webRequest = System.Net.WebRequest.Create (WEBSERVICE_URL);
if (webRequest != null) {
webRequest.Method = "POST";
//webRequest.Timeout = 12000;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Headers.Add ("Key", _apiKey);
webRequest.Headers.Add ("Sign", genHMAC ());
byte[] dataStream = Encoding.UTF8.GetBytes("command=returnBalances&nonce=" + nonce);
webRequest.ContentLength = dataStream.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(dataStream, 0, dataStream.Length);
newStream.Close();
using (System.IO.Stream s = webRequest.GetResponse ().GetResponseStream ()) {
using (System.IO.StreamReader sr = new System.IO.StreamReader (s)) {
var jsonResponse = sr.ReadToEnd ();
OutputText.text = jsonResponse.ToString ();
}
}
}
}
catch (Exception ex)
{
OutputText.text = ex.ToString ();
}
This gives me an "error(403) Forbidden" and the exception stack trace, but I am trying to get the 403 Response Body:
I cannot get Unity C# to accept the following code: mentioned here
catch(WebException ex)
{
var response = (HttpWebResponse)ex.Response;
}
What do I need to make this method work? I need the Response body
hmac.ToString()
will not generate valid sign. To generate valid sign for Poloniex, you must use HMACSHA512.ComputeHash method and pass it dataStream
as an argument. Bytes returned from this method then must be converted to hexadecimal string.
This should generate valid sign:
var hmac = new HMACSHA512(APISecret_Bytes);
var signBytes = hmac.ComputeHash(dataStream);
var sign = String.Join("", signBytes.Select(b => b.ToString("X2")));
Regarding WebException, it is declared in System.Net
namespace, so make sure you have using System.Net;
. Depending on target framework, WebException shoul be in System.Net.Requests.dll
, System.dll
, or netstandard.dll
. Unfortunatelly I don't have any experience with Unity, but if my guess that it targets ordinary .NET Framework
is correct, then WebException is declared in System.dll
and your project should already have reference to it and no additional assembly reference should be needed.