public virtual PingbackResult ReceivePingback(Id entryId, Uri sourceUrl, Uri targetUrl, string ip, Uri referrer) { LogService.Info("TrackbackService.ReceivePingback entryId={0} sourceUrl={1} targetUrl={2}", entryId, sourceUrl, targetUrl); if (!IsEnabled(AtomEntryRepository.GetEntry(entryId))) { PingbackResult result = new PingbackResult(); result.SetFault(49, "Pingbacks are turned off."); return result; } try { string page = string.Empty; string contentType = "text/html"; using (WebClient client = new WebClient()) { page = client.DownloadString(sourceUrl); if (client.ResponseHeaders["Content-Type"] != null) contentType = client.ResponseHeaders["Content-Type"]; } //validate page has a link back //if (!ContainsLink(page, RouteService.IdToWebHref(entryId, null))) // throw new AnnotationNotAllowedException(entryId, "trackback", "it does not link back"); AtomEntry pingback = new AtomEntry(); //content pingback.Content = new AtomContent() { Src = sourceUrl, Type = contentType }; //title string title = null; if (title == null) title = WebHelper.ExtractTitleForPage(page); if (title == null) title = "Pingback"; pingback.Title = new AtomTitle() { Text = title }; //summary string summary = WebHelper.ExtractDescriptionForPage(page); if (summary == null) summary = string.Empty; pingback.Summary = new AtomSummary() { Text = summary }; //author pingback.Authors = new List<AtomPerson>() { new AtomAuthor() { Name = string.Empty, Uri = referrer } }; //add extension data? pingback.SetValue<string>(Atom.SvcNs + "ip", ip); pingback.AnnotationType = "pingback"; //TODO: turn this into dependency AnnotateService.Annotate(entryId, pingback, null); return new PingbackResult() { Success = "Success" }; } catch (Exception ex) { LogService.Error(ex); PingbackResult r = new PingbackResult(); r.SetFault(0, ex.Message); return r; } }
public virtual PingbackResult SendPingback(AtomEntry entry, Uri pingUrl, Uri pageUrl) { LogService.Info("TrackbackService.SendPingback entryId={0} pingUrl={1} pageUrl={2}", entry.Id, pingUrl, pageUrl); try { var url = new UrlHelper(Container.GetInstance<RequestContext>()); using (WebClient client = new WebClient()) { string response = client.UploadString(pingUrl, new XElement("methodCall", new XElement("methodName", "pingback.ping"), new XElement("params", new XElement("param", new XElement("value", new XElement("string", url.RouteIdUrl("BlogEntry", entry.Id, AbsoluteMode.Force)))), new XElement("param", new XElement("value", new XElement("string", pageUrl))))).ToString()); return new PingbackResult() { Xml = XElement.Parse(response.Substring(response.IndexOf('<'))) }; } } catch (Exception ex) { LogService.Error(ex); PingbackResult r = new PingbackResult(); r.SetFault(0, ex.Message); return r; } }