Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
So I found this website http://ip-api.com/csv/208.80.152.201[^] but the thing is everything's mixed.
I only want to get the country name after "success," word.
Any ideas on how to do this?

What I have tried:

Dim client As WebClient = New WebClient()
Dim reply As String = client.DownloadString("http://ip-api.com/csv/208.80.152.201")
textbox1.Text = reply
Posted
Updated 20-Sep-16 21:18pm

1 solution

if you look at the api doc (for csv) IP Geolocation API - CSV [ip-api][^] you'll see that the return info is

success,COUNTRY,COUNTRY CODE,REGION CODE,REGION NAME,CITY,ZIP CODE,LATITUDE,LONGITUDE,TIME ZONE,ISP NAME,ORGANIZATION NAME,AS NUMBER / NAME,IP ADDRESS USED FOR QUERY

so, if you do a string.Split ie

char[] delimiterChars = { ',' };
String[] csvElements = reply.Split(delimiterChars);

string ipCountry = csvElements[1];


or

string ipCountry = csvElements[2];


IF csvElements[0] == "success" that is

You can wrap the whole thing into a function, pass in int 1 or 2 for 'which' country version you want back, returned as a string, or perhaps string.Empty if not 'success'

[edit] something like this (not compiled or tested)

static string getCountryFromReply(string reply, int countryVersion)
{
	if (!(reply.Trim().StartsWith("success")))
	{
		return string.Empty;
	}

	if (countryVersion != 1 && countryVersion != 2)
	{
		throw new argumentException("Only 1 or 2 Valid", "countryVersion");
	}

	char[] delimiterChars = {‘,’};
	string[] csvElements = reply.Split(delimiterChars);

	return csvElements(countryVersion);
}

[/edit]

is what I had in mind - you'll need to retype it though and check it carefully, the "" are from my Mac, VS will likely swear at you, but thats the gist - you could also make countryVersion an enum to make it easier to remember,

enum countryVersionReturn {countryLong=1, countryShort};


if you dont like any of this, then, you can also look at just requesting the fields you want IP Geolocation API - Return values [ip-api][^] in your case country,countryCode, Im not sure if that returns the status ie success / fail, I would always include the status field anyway, which means you still have to split the string
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900