![]() |
Tutorial Link Shortener / API Based Result Handling - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework) +--- Thread: Tutorial Link Shortener / API Based Result Handling (/Thread-Tutorial-Link-Shortener-API-Based-Result-Handling) |
Link Shortener / API Based Result Handling - PhäntömZ - 08-21-2013
API Based Results Tutorial
Summary In this tutorial, I hope to help you better understand web client usage, API response handling, and general API usage. API's are very helpful for sending short, quick requests and having a server handle most of the work for you, while also making it easier on a programmer to do their job. We're going to start off with creating the new program, designing the UI, then sending the request while looking at some extra options for the webclient, and finally parsing the result. Shortly after this tutorial is posted, I will post an API creation tutorial in the PHP/"Web" section. We are going to be using my API to shorten URLs in this tutorial. API Application programming interface Uses
Required Materials
Alright, now that I passed along that (mostly) useless information, let's get started! Step 1: A New Project Firstly, you will need to make a new Visual Basic Windows Forms Application. You can name it anything you like, it does not affect the project. Spoiler: Step 2: GUI Design Now we need to build the GUI. You will need the following controls:
Lay them out any way you wish, but this is how I laid mine out: Spoiler: Now, let's do some renaming and re-texting. Change the first label to: "Input" and the second label to "Result". Change the form title to whatever you like. Change the button text to "Shorten". Alright, now we have to RENAME the two text boxes and the button. Click on the first text box and find the name property on the "Properties" tab: ![]() Rename the first text box to: txtInput Rename the second text box to: txtResult Rename the button to: btnShorten *computer just crashed, so I had to remake my project ![]() Alright, now that the controls are renamed, on to the coding! Step 3: Coding Now we learn how to actually interact with the API using a web client! Double click the button, and you should be brought to the code editor which should look exactly like this: Spoiler: Before we can use any web clients, we first need to import System.Net. So go to line one and free up a few lines and type: Code: Imports System.Net You code page should now look like this: Spoiler: Now we are able to use web clients in our project. In order to do that, we must first declare a new web client. But before doing that, let's check if the user put anything into the input text box before doing unnecessary work. Put the following code into the sub: Code: If txtInput.Text = Nothing Then That will make sure that the user of the program actually put in a URL to be shortened. After that code, let's declare the WebClient. Code: Dim Shorten as New WebClient Now, if you wanted to do some basic protection from Fiddler if this was a sensitive request, you could do: Code: Shorten.Proxy = Nothing You can also assign custom user agents for your WebClients as well. But moving on. We want to have some basic error handling in case anything goes wrong, so let's put in a "Try" statement in case anything goes wrong: Code: Try Notice how I put in the message box explaining that an error occurred, along with displaying the actual error as the text of the message box. Now, to actually use the WebClient we use the following code: Code: txtResult.Text = Shorten.DownloadString("http://my-x-api.tk/us/short.php?url=" & txtInput.Text) We are feeding the results of the request directly into the text box for ease of use. Shorten is the name of the WebClient, the Quote:http://my-x-api.tk/us/short.phpis my API ( that I coded for shortening URLs ) that we are calling with the WebClient, and the Code: & txtInput.Text Now, your code should look like this: Spoiler: Congratulations, you have just finished a working URL shortener! Just press F5 to debug and test it and you are good to go! Thank you for reading this tutorial and I hope you enjoy it. Please leave constructive criticism! RE: Link Shortener / API Based Result Handling - Sapientia - 08-21-2013 Nice, and detailed tutorial! Liking it buddy!
RE: Link Shortener / API Based Result Handling - ErroraBorealis - 08-21-2013 Very nice tut, and it has some good uses other than the link shortener. I might use this for a private booter myself. RE: Link Shortener / API Based Result Handling - Nefarious - 08-21-2013 This is actually a nice tutorial, thanks man. |