我正在使用此代码获取有关我的IP地址的信息
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();
}
返回label1
var sGeobytesLocationCode="PKPBRAWA";
var sGeobytesIsLocationMatch=false;
var sGeobytesCountry="Pakistan";
var sGeobytesRegion="Punjab";
var sGeobytesCity="Rawalpindi";
var sGeobytesLatitude="33.6000";
var sGeobytesLongitude="73.0670";
如何从中获取城市名称。就像我的案例拉瓦尔品第
答案 0 :(得分:1)
试试这个
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 ;
}
添加名称空间using System.Text.RegularExpressions
希望得到这个帮助。