Reputation:
I am using this code to get information about my ip address
protected void Page_Load(object sender, EventArgs e)
{
WebRequest request = WebRequest.Create("http://gd.geobytes.com/gd?after=-1&variables=GeobytesCountry,GeobytesCity,GeobytesRegion,GeobytesLatitude,GeobytesLongitude");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
var responseFromServer = reader.ReadToEnd();
Label1.Text = responseFromServer.ToString();
reader.Close();
dataStream.Close();
response.Close();
}
It returns to label1
var sGeobytesLocationCode="PKPBRAWA";
var sGeobytesIsLocationMatch=false;
var sGeobytesCountry="Pakistan";
var sGeobytesRegion="Punjab";
var sGeobytesCity="Rawalpindi";
var sGeobytesLatitude="33.6000";
var sGeobytesLongitude="73.0670";
How do I get only the city name from this. As in my case Rawalpindi
Upvotes: 2
Views: 346
Reputation: 1456
Try this
string temp = responseFromServer.ToString();
Match _matchdec = Regex.Match(temp, @"\ssGeobytesCity=""\b(\S*)\b""", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
if (_matchdec.Success)
{
string retval = _matchdec.Groups[1].Value;
Label1.Text = retval ;
}
Add namespace using System.Text.RegularExpressions
Hope this help.
Upvotes: 1