Uri FindTargetUrl(Uri sendUrl, HttpWebResponseAbstraction response) { string ping_url = response.Headers["x-pingback"]; if (!String.IsNullOrEmpty(ping_url)) { return(new Uri(ping_url)); } Stream receiveStream = response.GetResponseStream(); if (response.ContentEncoding.ToUpperInvariant().Contains("GZIP")) { receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress); } else if (response.ContentEncoding.ToUpperInvariant().Contains("DEFLATE")) { receiveStream = new DeflateStream(receiveStream, CompressionMode.Decompress); } HtmlDocument html = new HtmlDocument(); html.Load(receiveStream); var node = html.DocumentNode.SelectSingleNode("//link[@rel='pingback' and @href]"); if (node != null) { return(new Uri(node.Attributes["href"].Value)); } throw new LinkbackSendException(String.Format(CultureInfo.InvariantCulture, "{0} url discovering failed for {1}", Name, sendUrl)); }
void SendToTarget(Uri targetUrl, LinkbackSendParameters parameters) { HttpWebRequestAbstraction request = HttpWebRequestAbstraction.Create(targetUrl); SetupRequest(request, parameters); using (HttpWebResponseAbstraction response = request.GetResponse()) { if (!response.IsHttpStatusCode2XX) { throw new LinkbackSendException(String.Format(CultureInfo.InvariantCulture, "Http error while sending {0} for {1}", Name, targetUrl)); } Stream receiveStream = response.GetResponseStream(); if (response.ContentEncoding.ToUpperInvariant().Contains("GZIP")) { receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress); } else if (response.ContentEncoding.ToUpperInvariant().Contains("DEFLATE")) { receiveStream = new DeflateStream(receiveStream, CompressionMode.Decompress); } StreamReader streamReader = new StreamReader(receiveStream, Encoding.UTF8); ParseResponse(targetUrl, streamReader.ReadToEnd()); } }
protected string DownloadSourceContent(Uri sourceUrl) { HttpWebRequestAbstraction request = HttpWebRequestAbstraction.Create(sourceUrl); using (HttpWebResponseAbstraction response = request.GetResponse()) { if (response.IsHttpStatusCode2XX) { Stream receiveStream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(receiveStream, Encoding.UTF8); return(streamReader.ReadToEnd()); } } throw new LinkbackReceiveException(String.Format(CultureInfo.InvariantCulture, "Http error while discovering {0} source at {1}", Name, sourceUrl)); }
Uri FindTargetUrl(Uri sendUrl, LinkbackSendParameters parameters, HttpWebResponseAbstraction response) { Stream receiveStream = response.GetResponseStream(); if (response.ContentEncoding.ToUpperInvariant().Contains("GZIP")) { receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress); } else if (response.ContentEncoding.ToUpperInvariant().Contains("DEFLATE")) { receiveStream = new DeflateStream(receiveStream, CompressionMode.Decompress); } StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); string content = readStream.ReadToEnd(); var matches = _regex_rdf.Matches(content); if (matches.Count > 0) { foreach (Match match in matches) { if (String.Compare(match.Groups[1].ToString().Trim(), sendUrl.ToString(), StringComparison.OrdinalIgnoreCase) == 0) { string trackback_ping_url = match.Groups[2].ToString().Trim(); return(new Uri(trackback_ping_url, UriKind.Absolute)); } } } if (parameters.AutoDiscovery == null) { return(sendUrl); } throw new LinkbackSendException(String.Format(CultureInfo.InvariantCulture, "RDF not found while discovering {0} url for {1}", Name, sendUrl)); }