<%@ WebService Language="C#" Class="TrafficService" %>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
[WebService(Namespace = "http://maslan.org/webservices/")]
public class TrafficService
{
public TrafficService()
{
}
[WebMethod]
public DriveTime GetDriveTime(string url511)
{
string result = ReadHtmlPage(url511);
return GetTimeDistance(result);
}
private String ReadHtmlPage(string url)
{
String result;
string sProxyServer = "winisp-proxy";
int iProxyPort = 8080;
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objRequest.Proxy = new System.Net.WebProxy("http://" + sProxyServer + ":" + iProxyPort + "/", true);
objResponse = objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream(), System.Text.Encoding.UTF8))
{
result = sr.ReadToEnd();
sr.Close();
}
return result;
}
private DriveTime GetTimeDistance(String inputString)
{
Regex r;
Match m;
DriveTime result = new DriveTime();
int pos = 0; //track position in string just for slight matching efficiency
// get FROM location
r = new Regex(@"From:\S*\s*(?<1>[^\<]*)",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
m = r.Match(inputString, pos);
if (m.Success)
{
result.from = m.Groups[1].ToString().Replace(@"&", @"&");
pos = m.Index;
}
// get TO location
r = new Regex(@"To:\S*\s*(?<1>[^\<]*)",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
m = r.Match(inputString, pos);
if (m.Success)
{
result.to = m.Groups[1].ToString().Replace(@"&", @"&");
pos = m.Index;
}
// get TIME and DISTANCE
r = new Regex(@"Trip A:\s*(?<1>[^\.]*)[^(]*[(](?<2>[^)]*)",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
// though not used now, for loop left here for future multi-segment matching
for (m = r.Match(inputString, pos); m.Success; m = m.NextMatch())
{
result.driveTime = m.Groups[1].ToString();
result.distance = m.Groups[2].ToString();
}
return result;
}
}
public class DriveTime
{
public string from;
public string to;
public string driveTime;
public string distance;
}