Make an authenticated #Http Request with #Kotlin

January 19, 2018 Leave a comment

kotlin_250x250

Kotlin is a superscript of Java, and the next big thing in Android programming, so it’s worth taking a little dabble in the language, to do something basic.

Beyond Hello World, I always see if I can use it to call an API, which of course, my example below is of RegCheck.org.uk – a car registration API. You’ll need to get your own username and password at the website to try out the below code.

Create a file called Http.kt as below;

import java.io.IOException
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

fun main(vararg args: String) {
val response = sendGet(“https://www.regcheck.org.uk/api/json.aspx/Check/LA15ECT”,”**username**”,”**password**”);
println(response);
}

 

private fun sendGet(url: String, username: String, password: String ) : String {
val connection = URL(url).openConnection() as HttpURLConnection
val auth = Base64.getEncoder().encode((username + “:” + password).toByteArray()).toString(Charsets.UTF_8)
connection.addRequestProperty(“Authorization”, “Basic $auth”)
connection.connect();
val text = connection.inputStream.use { it.reader().use { reader -> reader.readText() } }
return text;
}

Compile the JAR using the code;

kotlinc http.kt -include-runtime -d http.jar

Then run it by using;

java -jar http.jar

and you should get the output

“ABICode”:”21057867″,”Description”:”2015 Honda Civic Type R Gt, 1996CC Petrol, 5DR, Manual”,”RegistrationYear”:”2015″,”CarMake”:{“CurrentTextValue”:”Honda”},”CarModel”:{“CurrentTextValue”:”Civic”},”EngineSize”:{“CurrentTextValue”:”1996CC”},”FuelType”:{“CurrentTextValue”:”Petrol”},”MakeDescription”:”Honda”,”ModelDescription”:”Civic”,”Immobiliser”:{“CurrentTextValue”:””},”NumberOfSeats”:{“CurrentTextValue”:4},”IndicativeValue”:{“CurrentTextValue”:””},”DriverSide”:{“CurrentTextValue”:”RHD”},”Transmission”:{“CurrentTextValue”:”Manual”},”NumberOfDoors”:{“CurrentTextValue”:”5″},”ImageUrl”:”http://www.regcheck.org.uk/image.aspx/@SG9uZGEgQ2l2aWM=”,”VehicleInsuranceGroup”:”21″}

Advertisements
Categories: Uncategorized

#Backup #AWS #Route53 DNS Zones in C#

January 16, 2018 1 comment

route53

If you host your DNS on Amazon Route53, and it’s reasonably complex, it’s worthwhile taking a backup of it, so that future errors can be undone quickly. There are some tools to do it, however, you can also write a script yourself in C# to do it.

First off, get the NUGET package

Install-Package AWSSDK

Create an IAM user within AWS with programmatic access, and get it’s Access Key and Secret ID. – I’ve given it Administrator access, but you should limit the scope to the minimum.

Then I output all the zones to the console here;

var client = AWSClientFactory.CreateAmazonRoute53Client(“***”, “***”, RegionEndpoint.USEast1);

var strOutput = @”route53backup.json”;

var req = new ListHostedZonesRequest();
for (; ; )
{
var zones = client.ListHostedZones(req);
foreach (HostedZone zone in zones.HostedZones)
{

Console.WriteLine(zone.Name);

var recordSets = client.ListResourceRecordSets(new ListResourceRecordSetsRequest() { HostedZoneId = zone.Id });

foreach (var record in recordSets.ResourceRecordSets)
{
Console.WriteLine(“\t” + record.Name + ” (” + record.Type + “)”);

foreach (var resource in record.ResourceRecords)
{
Console.WriteLine(“\t\t” + resource.Value);

}
}
}
req.Marker = zones.NextMarker;
if (req.Marker == null)
{
break;
}
}

Note that the ListHostedZones call only returns 100 domains at a time, you have to set the “marker” parameter to the value of the NextMarker property that was returned on the previous call to get all records.

 

Categories: Uncategorized

Free HTTPS image hosting – website revamp

January 10, 2018 Leave a comment

httpsimage

If you need to host an image via HTTPS (SSL), because it is going to be embedded into a secure website, such as paypal, ebay, or an email footer. Then this website httpsimage.com is a really easy and free way to upload a single image for hosting online via HTTPS.

The website just got revamped, with the new Responsive MaterializeCSS framework, and Stripe to handle the payments.

Over the next few days, there are going to be new features added, like bulk uploads, better logging, and more…

 

Categories: Uncategorized

Access an #Oracle 11g Database via an #iOS app.

January 10, 2018 Leave a comment

This is a short video demoing the iOS App “”MsSQL,MySQL,Oracle & Postgre” available via https://itunes.apple.com/gb/app/mssql… It shows how to add an Oracle Database server, view tables, and query them, all under 30 seconds!

Categories: Uncategorized

Connecting to #PostgreSQL via C#

December 19, 2017 1 comment

0107.sdt-postgre

As a planned expansion to the Mobile SQL client app (https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=1325541368&mt=8) for iOS, – soon support for Oracle and Postgre will be added.

The back-end to the app is some C# middleware, and the code it uses to connect to Postgre is as follows;

private static DataTable ExecutePostgre(string strDataSource, string databaseName, string strUsername, string strPassword, string strSQL)
{
const string strDsnTemplate = @”Server={0};User Id={1}; Password={2};Database={3};”;
var sqlConnection = string.Format(strDsnTemplate, strDataSource, strUsername, strPassword, databaseName);
var adapter = new NpgsqlDataAdapter(strSQL, sqlConnection) { SelectCommand = { CommandTimeout = 0 } };
var dataSet = new DataSet();
adapter.Fill(dataSet, “sql”);
return dataSet.Tables[“sql”];
}

Where NpgsqlDataAdapter comes from the Nuget Package Npgsql, which is installed using

Install-Package Npgsql

Categories: Uncategorized

Connect to an #Oracle #Database from C#

December 18, 2017 1 comment

oracle

Connecting to an Oracle Database in c# is just as easy as MySQL or SQL server, it just requires it’s own library and a different connection string.

First, you need the following Nuget Package

Install-Package Oracle.ManagedDataAccess

Then import it using:

using Oracle.ManagedDataAccess.Client;

Here’s a code example –

public static DataTable PopulateDataTable(string command)
{
var sqlConnection = @”Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));user id=system;Password=xyz123″;
var adapter = new OracleDataAdapter(command, sqlConnection) { SelectCommand = { CommandTimeout = 0 } };
var dataSet = new DataSet();
adapter.Fill(dataSet, “sql”);
return dataSet.Tables[“sql”];
}

Here, the default service is “XE”, the TNS port is 1521, the  username is system, and password is xyx123 – and the oracle server is on the same PC (localhost)

It’s used as follows;

var strSQL = “select * from someTable”;
var dt = PopulateDataTable(strSQL);
foreach(var dr in dt.Rows.Cast<DataRow>())
{
Console.WriteLine(dr[“someColumn”].ToString());
}

 

Categories: Uncategorized

Query #Microsoft #SQL server from an #iPhone

December 15, 2017 Leave a comment

This slideshow requires JavaScript.

If you use Microsoft SQL server as your production database server, and you keep getting calls when you are out-of-office to make simple updates or check a value in the database, then you no longer have to rush to your PC.

This app, just released yesterday – Called “MsSQL & MySQL Client” is available for download from the app store for 99p at

https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=1325541368&mt=8&at=1000l9tW

As the name suggests, it allows updates to both MySQL and SQL server databases.

 

Categories: Uncategorized