2

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

Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42
DroneLord
  • 53
  • 12
  • This means that you are not authorized to access the resource. You must test your credentials, do so by using postman just to verify and narrow what is the true issue. – ngeksyo Apr 14 '18 at 03:43
  • "cannot get Unity C# to accept" you mean you get a compile error? Specifically what? – ADyson Apr 14 '18 at 04:47
  • WebException shows up red, and my current exception (ex) doesn't provide an ex.Response for the type Exception, I think I'm missing a library maybe up where you type "using etc..." at the top of all the scripts – DroneLord Apr 14 '18 at 04:57
  • The way you generate *Sign* seems to be wrong. To generate valid sign for Poloniex, all POST data (your `dataStream` variable) must be used as an input for HMAC. You cannot generate sign before you have data to sign. – Ňuf Apr 14 '18 at 05:17
  • It is signed in my genHMAC() function here: private string genHMAC() { byte [] APISecret_Bytes = System.Text.Encoding.UTF8.GetBytes(_apiSecret); var hmac = new HMACSHA512(APISecret_Bytes); return hmac.ToString(); } – DroneLord Apr 14 '18 at 05:25

1 Answers1

1

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.

Ňuf
  • 6,027
  • 2
  • 23
  • 26