Jul 2, 2016 01:32 PM
(This post was last modified: Jul 2, 2016 01:32 PM by Skeetz.)
Sooo i have been fucking with this code for hours now and it is still giving me the error : Response String: The remote server returned an error: (400) Bad Request.
I dont understand why lbc is telling me the request i submitted was bad / invalid.
Code:
Form load:
HMAC FUNCTION:
I dont understand why lbc is telling me the request i submitted was bad / invalid.
Code:
Form load:
Code
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Uri As String
Dim path As String
Dim statuscode As HttpStatusCode
Dim dataStream As Stream = Nothing
Dim StreamReader As StreamReader
Dim ResponseFromServer As String
Dim ApiKey As String
Dim SecretKey As String
Dim uri_path As String
Dim total_key As String
Dim time_in_ms As DateTime = DateTime.Now
Uri = "https://localbitcoins.com"
path = "/api/myself/"
ApiKey = "publickey"
SecretKey = "secretkey"
uri_path = Uri & path
total_key = time_in_ms.Millisecond.ToString & ApiKey & path
Try
Dim request As WebRequest = WebRequest.Create(Uri_path)
request.Timeout = 120000
request.Method = "POST"
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(total_key)
request.Headers.Add("Apiauth-Key", ApiKey)
request.Headers.Add("Apiauth-Nonce", time_in_ms.Millisecond.ToString)
request.Headers.Add("Apiauth-Signature", HashHMAC(SecretKey, total_key).ToUpper)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = byteArray.Length
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim WebResponse As WebResponse = request.GetResponse()
dataStream = WebResponse.GetResponseStream()
StreamReader = New StreamReader(dataStream)
ResponseFromServer = StreamReader.ReadToEnd()
dataStream.Close()
WebResponse.Close()
statuscode = HttpStatusCode.OK
Catch ex As WebException
If ex.Response IsNot Nothing Then
dataStream = ex.Response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
statuscode = DirectCast(ex.Response, HttpWebResponse).StatusCode
ResponseFromServer = ex.Message
Else
Dim resp As String = ""
statuscode = HttpStatusCode.ExpectationFailed
ResponseFromServer = "Error"
End If
Catch ex As Exception
statuscode = HttpStatusCode.ExpectationFailed
ResponseFromServer = "Error"
End Try
'Console.WriteLine("Response Code: " & statuscode)
TextBox1.AppendText("Response String: " & ResponseFromServer)
'Console.WriteLine("Response String: " & ResponseFromServer)
End SubHMAC FUNCTION:
Code
Public Function HashHMAC(key As String, message As String) As String
Dim encoding As System.Text.UTF8Encoding
Dim keyByte As Byte()
Dim hmacsha256 As HMACSHA256
Dim messageBytes As Byte()
Dim hashmessage As Byte()
encoding = New System.Text.UTF8Encoding()
keyByte = encoding.GetBytes(key)
hmacsha256 = New HMACSHA256(keyByte)
messageBytes = encoding.GetBytes(message)
hashmessage = hmacsha256.ComputeHash(messageBytes)
Return ByteArrayToString(hashmessage)
End Function
Public Function ByteArrayToString(ba As Byte()) As String
Dim hex As StringBuilder
Dim b As Byte
hex = New StringBuilder(ba.Length * 2)
For Each b In ba
hex.AppendFormat("{0:x2}", b)
Next
Return hex.ToString()
End Function


