📄 serviceutility.cs
字号:
}
//SMR - Begin: fixing refresh behavior
// If we cached a failed response then it contains no data, don't return anything.
if(responseInfo.ServiceFailedLastCall) //SMR - fixing refresh behavior
{
return null;
}
//SMR - End: fixing refresh behavior
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);
//SMR - begin - fixing refresh behavior
if(rawResponse == "")
{
return null;
}
//SMR - end - fixing refresh behavior
// 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
//SMR - Begin: fixing refresh behavior
try
{
ServiceResponseInfo responseInfo = objService.GetCommunityContent();
// discard extras (over MaximumItems)
if (responseInfo.Items.Count > serviceInfo.MaximumItems)
responseInfo.Items = responseInfo.Items.GetRange(0, serviceInfo.MaximumItems);
return responseInfo;
}
catch(Exception ex)
{
ActivityUtility.RecordError("Web Service call failure: " + serviceInfo.Name, ex);
return null;
}
//SMR - End: fixing refresh behavior
}
//SMR - fixing refresh behavior
public static void UpdateServiceRefreshDate(string name, DateTime newDate)
{
SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesUpdateServiceRefreshDate", conPortal);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmd.Parameters.Add("@name", name);
cmd.Parameters.Add("@date", newDate);
conPortal.Open();
cmd.ExecuteNonQuery();
conPortal.Close();
}
//*********************************************************************
//
// ServiceUtility Constructor
//
// Use private constructor for class with static methods.
//
//*********************************************************************
private ServiceUtility() {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -