Results 1 to 4 of 4

Thread: [RESOLVED] Help converting c# to vb net code

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Posts
    76

    Resolved [RESOLVED] Help converting c# to vb net code

    I would like to use the c# code from this API here:
    https://bitbucket.org/arrivets/krake...e-view-default

    to request the POST data in a VB Net program I have. All I really need converted to VB NET is the code below:

    Code:
     private JsonObject QueryPrivate(string a_sMethod, string props = null)
            {
                // generate a 64 bit nonce using a timestamp at tick resolution
                Int64 nonce = DateTime.Now.Ticks;
                props =  "nonce=" + nonce + props;
    
    
                string path = string.Format("/{0}/private/{1}", _version, a_sMethod);
                string address = _url + path;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(address);
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.Method = "POST";
                webRequest.Headers.Add("API-Key", _key);
    
                
                byte[] base64DecodedSecred = Convert.FromBase64String(_secret);
    
                var np = nonce + Convert.ToChar(0) + props;
    
                var pathBytes = Encoding.UTF8.GetBytes(path);
                var hash256Bytes = sha256_hash(np);
                var z = new byte[pathBytes.Count() + hash256Bytes.Count()];
                pathBytes.CopyTo(z, 0);
                hash256Bytes.CopyTo(z, pathBytes.Count());
    
                var signature = getHash(base64DecodedSecred, z);
    
                webRequest.Headers.Add("API-Sign", Convert.ToBase64String(signature));
    
                if (props != null)
                {
    
                    using (var writer = new StreamWriter(webRequest.GetRequestStream()))
                    {
                        writer.Write(props);
                    }
                }
    
                //Make the request
                try
                {
                    //Wait for RateGate
                    _rateGate.WaitToProceed();
    
                    using (WebResponse webResponse = webRequest.GetResponse())
                    {
                        using (Stream str = webResponse.GetResponseStream())
                        {
                            using (StreamReader sr = new StreamReader(str))
                            {
                                return (JsonObject)JsonConvert.Import(sr);
                            }
                        }
                    }
                }
                catch (WebException wex)
                {
                    using (HttpWebResponse response = (HttpWebResponse)wex.Response)
                    {
                        using (Stream str = response.GetResponseStream())
                        {
                            using (StreamReader sr = new StreamReader(str))
                            {
                                if (response.StatusCode != HttpStatusCode.InternalServerError)
                                {
                                    throw;
                                }
                                return (JsonObject)JsonConvert.Import(sr);
                            }
                        }
                    }
    
                }
            }
    Now, I know I can throw this into a c# to vb net converter, but it doesn't work as well as I planned. I am having some issues with the SHA256 portion.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Help converting c# to vb net code

    The reason why it is likely failing when you put it through the converter is because it is implicitly declaring a variable that returns the value from a Function named sha256_hash. Do you have that particular Function declared?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Posts
    76

    Re: Help converting c# to vb net code

    AH yes silly mistake. I didn't read far enough through that C# example! We can close this out...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Help converting c# to vb net code

    Quote Originally Posted by hockeyadc View Post
    We can close this out...
    Then you should use the Thread Tools menu to mark the thread Resolved.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width