Sample source code
Use the code provided below as a starting point for creating your own
applications. We also have an open-source Kibot
API Client application written in .NET that further demonstrates the
communication between the client application and our servers. All data
from our servers can be sent in a compressed
format making the client-server communication and data download more
efficient.
The source code below performs the following actions:
- Logs in to the server using the
Guest account
- Downloads the last 10 days of
daily data for MSFT (Microsoft)
- Decompresses the Gzip data stream
automatically
- Saves decompressed data to a text file
VB.NET version of the source code
You can find the C# version of
the same code below.
Private Sub Download()
Dim UrlLogin As String
Dim UrlDataRequest As String
Dim FileName As String
Dim Request As System.Net.HttpWebRequest
Dim Response As System.Net.WebResponse
Dim Stream As System.IO.Stream
Dim FileStream As System.IO.FileStream
Dim Text As String
Dim UserAgent As String
Dim BytesRead As Integer
Dim Buffer As Byte()
UrlLogin = "http://api.kibot.com?action=login&" & _
"user=guest&password=guest"
UrlDataRequest = "http://api.kibot.com?action=history&" & _
"symbol=msft&interval=daily&period=10"
FileName = "c:\MSFT.txt"
UserAgent = "Your application name or id"
'Login to the server first
Try
Request = System.Net.WebRequest.Create(UrlLogin)
Request.UserAgent = UserAgent
Response = Request.GetResponse
Text = New System.IO.StreamReader(Response.GetResponseStream, _
System.Text.Encoding.ASCII).ReadToEnd
'Check response
If Not Text.StartsWith("200") Then
Throw New Exception("Cannot login to the server. " & Text)
End If
Catch ex As System.Net.WebException
Throw ex
End Try
'Delete existing file
If System.IO.File.Exists(FileName) Then
System.IO.File.Delete(FileName)
End If
'Download data
Try
Request = System.Net.WebRequest.Create(UrlDataRequest)
Request.AutomaticDecompression = _
System.Net.DecompressionMethods.Deflate Or _
System.Net.DecompressionMethods.GZip
Request.Headers.Add("Accept-Encoding", "gzip,deflate")
Request.UserAgent = UserAgent
FileStream = System.IO.File.Create(FileName)
Response = Request.GetResponse()
Stream = Response.GetResponseStream()
Buffer = New Byte(4 * 1024 - 1) {}
BytesRead = Stream.Read(Buffer, 0, Buffer.Length)
While BytesRead > 0
FileStream.Write(Buffer, 0, BytesRead)
BytesRead = Stream.Read(Buffer, 0, Buffer.Length)
End While
Catch ex As System.Net.WebException
Throw ex
Finally
If FileStream IsNot Nothing Then
FileStream.Close()
End If
End Try
End Sub
C# version of the source code:
private void Download()
{
string UrlLogin = null;
string UrlDataRequest = null;
string FileName = null;
System.Net.HttpWebRequest Request = null;
System.Net.WebResponse Response = null;
System.IO.Stream Stream = null;
System.IO.FileStream FileStream = null;
string Text = null;
string UserAgent = null;
int BytesRead = 0;
byte[] Buffer = null;
UrlLogin = "http://api.kibot.com?action=login" +
"&user=guest&password=guest";
UrlDataRequest = "http://api.kibot.com?action=history" +
"&symbol=msft&interval=daily&period=10";
FileName = "c:\\MSFT.txt";
UserAgent = "Your application name or id";
//Login to the server first
try {
Request = System.Net.WebRequest.Create(UrlLogin);
Request.UserAgent = UserAgent;
Response = Request.GetResponse();
Text = new System.IO.StreamReader(Response.GetResponseStream(),
System.Text.Encoding.ASCII).ReadToEnd();
//Check response
if (!Text.StartsWith("200")) {
throw new Exception("Cannot login to the server. " + Text);
}
} catch (System.Net.WebException ex) {
throw ex;
}
//Delete existing file
if (System.IO.File.Exists(FileName)) {
System.IO.File.Delete(FileName);
}
//Download data
try {
Request = System.Net.WebRequest.Create(UrlDataRequest);
Request.AutomaticDecompression =
System.Net.DecompressionMethods.Deflate |
System.Net.DecompressionMethods.GZip;
Request.Headers.Add("Accept-Encoding", "gzip,deflate");
Request.UserAgent = UserAgent;
FileStream = System.IO.File.Create(FileName);
Response = Request.GetResponse();
Stream = Response.GetResponseStream();
Buffer = new byte[4 * 1024];
BytesRead = Stream.Read(Buffer, 0, Buffer.Length);
while (BytesRead > 0) {
FileStream.Write(Buffer, 0, BytesRead);
BytesRead = Stream.Read(Buffer, 0, Buffer.Length);
}
} catch (System.Net.WebException ex) {
throw ex;
} finally {
if (FileStream != null) {
FileStream.Close();
}
}
}
Historical Data API - Table of Contents