📄 serviceutility.cs
字号:
while (dr.Read())
colServices.Add(PopulateServiceInfoFromSqlDataReader(dr));
conPortal.Close();
return colServices;
}
//*********************************************************************
//
// GetAllCommunityServices Method
//
// Returns a list of all the services created for this community
// excluding RSS services.
//
//*********************************************************************
public static ArrayList GetAllCommunityServices() {
ArrayList colServices = new ArrayList();
SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesGetCommunityServices", conPortal);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
conPortal.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
colServices.Add(PopulateServiceInfoFromSqlDataReader(dr));
conPortal.Close();
return colServices;
}
//*********************************************************************
//
// PopulateServiceInfoFromSqlDataReader Method
//
// Creates a ServiceInfo object from a SqlDataReader.
//
//*********************************************************************
private static ServiceInfo PopulateServiceInfoFromSqlDataReader(SqlDataReader dr) {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.ID = (int)dr["service_id"];
serviceInfo.Name = (string)dr["service_name"];
serviceInfo.Url = (string)dr["service_url"];
serviceInfo.Password = (string)dr["service_password"];
serviceInfo.Type = (ServiceType)dr["service_type"];
serviceInfo.MaximumItems = (int)dr["service_maximumItems"];
serviceInfo.RefreshRate = (int)dr["service_refreshRate"];
serviceInfo.DateLastRefreshed = (DateTime)dr["service_dateLastRefreshed"];
return serviceInfo;
}
//*********************************************************************
//
// ClearWebServiceBoxFromCache Method
//
// Removes a single Web Service Box for cache.
//
//*********************************************************************
public static void ClearWebServiceBoxFromCache(string serviceName) {
string cacheKey = CommunityGlobals.CacheKey("WebServiceBox_" + serviceName);
HttpContext.Current.Cache.Remove(cacheKey);
}
//*********************************************************************
//
// GetServiceResponseInfo Method
//
// This is the main method for calling a service. The method
// attempts to get a service response from the cache. If it
// can't get the response from the cache, and the last refresh
// duration has passed, the service is called directly.
//
// Note: if the cache is dumped before the RefreshRate, then
// this method will return null. This method was designed to
// guarantee that the service will not be called more often
// than the refresh rate dictates.
//
//*********************************************************************
public static ServiceResponseInfo GetServiceResponseInfo(string name) {
// Get abbreviated reference to the current context
HttpContext context = HttpContext.Current;
// Get the cache key
string cacheKey = CommunityGlobals.CacheKey("WebServiceBox_" + name);
// Attempt to retrieve the Service Response from cache
ServiceResponseInfo responseInfo = (ServiceResponseInfo)context.Cache[cacheKey];
// If can't get from cache, better call the service
if (responseInfo == null) {
// Retrieve service info from DB
ServiceInfo serviceInfo = GetServiceInfoForRefresh(name);
// Don't refresh if under time limit
if (serviceInfo.DateLastRefreshed.AddMinutes(serviceInfo.RefreshRate) > DateTime.Now)
return null;
// Call the service
responseInfo = CallService(serviceInfo);
if (responseInfo == null)
return null;
// Update date last refreshed
responseInfo.DateLastRefreshed = DateTime.Now;
context.Cache.Insert
(
cacheKey,
responseInfo,
null,
DateTime.Now.AddMinutes(serviceInfo.RefreshRate),
Cache.NoSlidingExpiration,
CacheItemPriority.High,
null
);
}
return responseInfo;
}
//*********************************************************************
//
// CallService Method
//
// If a ServiceResponse cannot be retrieved from the cache,
// then the service is called directly by this method.
//
//*********************************************************************
private static ServiceResponseInfo CallService(ServiceInfo serviceInfo) {
ServiceResponseInfo responseInfo = null;
if (serviceInfo.Type == ServiceType.RSS)
responseInfo = CallRssService(serviceInfo);
else
responseInfo = CallCommunityService(serviceInfo);
return responseInfo;
}
//*********************************************************************
//
// CallRssService Method
//
// Calls an RSS service and returns the service response.
// This method goes crazy with the Regex class to parse the response
// from an RSS service. After testing, I discovered that most
// RSS services do not return a valid XML response. That requires
// parsing through possibly faulty XML (missing end tags, etc.)
//
//*********************************************************************
private static ServiceResponseInfo CallRssService(ServiceInfo serviceInfo) {
ServiceResponseInfo responseInfo = new ServiceResponseInfo();
// Grab the page
string rawResponse = GetWebPage(serviceInfo.Url);
// Get Channel
Match channel = Regex.Match(rawResponse, @"<channel>(.*?)</channel>", RegexOptions.Singleline);
if (channel.Success) {
// Get Service title
Match serviceTitle = Regex.Match(channel.Result("$1"), @"<title>(.*?)</title>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (serviceTitle.Success)
responseInfo.ServiceTitle = serviceTitle.Result("$1");
// Get Service Link
Match serviceLink = Regex.Match(channel.Result("$1"), @"<link>(.*?)</link>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (serviceLink.Success)
responseInfo.ServiceLink = serviceLink.Result("$1");
// Get Service Description
Match serviceDescription = Regex.Match(channel.Result("$1"), @"<description>(.*?)</description>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (serviceDescription.Success)
responseInfo.ServiceDescription = serviceDescription.Result("$1");
// Get Service Copyright
Match serviceCopyright = Regex.Match(channel.Result("$1"), @"<copyright>(.*?)</copyright>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (serviceCopyright.Success)
responseInfo.ServiceCopyright = serviceCopyright.Result("$1");
}
// Get Image
Match image = Regex.Match(rawResponse, @"<image>(.*?)</image>", RegexOptions.Singleline);
if (image.Success) {
// Get Image title
Match imageTitle = Regex.Match(image.Result("$1"), @"<title>(.*?)</title>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (imageTitle.Success)
responseInfo.ServiceImageTitle = imageTitle.Result("$1");
// Get Image Url
Match imageUrl = Regex.Match(image.Result("$1"), @"<url>(.*?)</url>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (imageUrl.Success)
responseInfo.ServiceImageUrl = imageUrl.Result("$1");
// Get Image Link
Match imageLink = Regex.Match(image.Result("$1"), @"<link>(.*?)</link>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (imageLink.Success)
responseInfo.ServiceImageLink = imageLink.Result("$1");
}
// Get Items
MatchCollection items = Regex.Matches(rawResponse, @"<item>(.*?)</item>", RegexOptions.Singleline);
int counter = 0;
foreach (Match item in items) {
counter ++;
if (counter > serviceInfo.MaximumItems)
break;
ServiceResponseInfoItem responseInfoItem = new ServiceResponseInfoItem();
// Get Item title
Match itemTitle = Regex.Match(item.Result("$1"), @"<title>(.*?)</title>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (itemTitle.Success)
responseInfoItem.Title = itemTitle.Result("$1");
// Get Item Link
Match itemLink = Regex.Match(item.Result("$1"), @"<link>(.*?)</link>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (itemLink.Success)
responseInfoItem.Link = itemLink.Result("$1");
// Get Item Description
Match itemDescription = Regex.Match(item.Result("$1"), @"<description>(.*?)</description>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (itemDescription.Success)
responseInfoItem.Description = itemDescription.Result("$1");
// Add new item to Response Info
responseInfo.Items.Add(responseInfoItem);
}
return responseInfo;
}
//*********************************************************************
//
// GetWebPage Method
//
// Retrieves a Web page that represents an RSS response.
// If no reply in 15 seconds, it gives up.
//
//*********************************************************************
private static string GetWebPage(string url) {
StringBuilder builder = new StringBuilder();
WebRequest req = WebRequest.Create(url);
// Set Timeout to 15 seconds
req.Timeout = 15000;
try {
WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Byte[] read = new Byte[512];
int bytes = ReceiveStream.Read(read, 0, 512);
while (bytes > 0) {
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
builder.Append(encode.GetString(read, 0, bytes));
bytes = ReceiveStream.Read(read, 0, 512);
}
}
catch(Exception){}
return builder.ToString();
}
//*********************************************************************
//
// CallCommunityService Method
//
// Calls a community Web service. This method uses a Web service
// proxy class to call a community service from (possibly) another
// community Web site.
//
//*********************************************************************
private static ServiceResponseInfo CallCommunityService(ServiceInfo serviceInfo) {
// instantiate the community web service proxy class
Client.CommunityService objService = new Client.CommunityService();
// Set the url
objService.Url = serviceInfo.Url;
// Set password
if (serviceInfo.Password.Trim() != String.Empty) {
Client.SecurityHeader objHeader = new Client.SecurityHeader();
objHeader.Password = serviceInfo.Password;
objService.SecurityHeaderValue = objHeader;
}
// return result
ServiceResponseInfo responseInfo = objService.GetCommunityContent();
// discard extras (over MaximumItems)
if (responseInfo.Items.Count > serviceInfo.MaximumItems)
responseInfo.Items = responseInfo.Items.GetRange(0, serviceInfo.MaximumItems);
return responseInfo;
}
//*********************************************************************
//
// ServiceUtility Constructor
//
// Use private constructor for class with static methods.
//
//*********************************************************************
private ServiceUtility() {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -