温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:LiveBlog v1.0测试版源码
当前文件:
LiveBlog/LiveBlog.Core/Ping/Pingback.cs,打开代码结构图
LiveBlog/LiveBlog.Core/Ping/Pingback.cs,打开代码结构图1Using 12
13
namespace LiveBlog.Core.Ping 14
{ 15
/// <summary> 16
/// Sends pingbacks to website that the blog links to. 17
/// </summary> 18
public static class Pingback 19
{ 20
21
/// <summary> 22
/// Sends pingbacks to the targetUrl. 23
/// </summary> 24
public static void Send(Uri sourceUrl, Uri targetUrl) 25
{ 26
if (!BlogSettings.Instance.EnablePingBackSend) 27
return; 28
29
if (sourceUrl == null || targetUrl == null) 30
return; 31
32
try 33
{ 34
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl); 35
request.Credentials = CredentialCache.DefaultNetworkCredentials; 36
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 37
string pingUrl = response.Headers["x-pingback"]; 38
Uri url; 39
if (!string.IsNullOrEmpty(pingUrl) && Uri.TryCreate(pingUrl, UriKind.Absolute, out url)) 40
{ 41
OnSending(url); 42
request = (HttpWebRequest)HttpWebRequest.Create(url); 43
request.Method = "POST"; 44
request.Timeout = 10000; 45
request.ContentType = "text/xml"; 46
request.ProtocolVersion = HttpVersion.Version11; 47
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)"; 48
AddXmlToRequest(sourceUrl, targetUrl, request); 49
request.GetResponse(); 50
OnSent(url); 51
} 52
} 53
catch (Exception) 54
{ 55
// Stops unhandled exceptions that can cause the app pool to recycle 56
} 57
} 58
59
/// <summary> 60
/// Adds the XML to web request. The XML is the standard 61
/// XML used by RPC-XML requests. 62
/// </summary> 63
private static void AddXmlToRequest(Uri sourceUrl, Uri targetUrl, HttpWebRequest webreqPing) 64
{ 65
Stream stream = (Stream)webreqPing.GetRequestStream(); 66
using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.ASCII)) 67
{ 68
writer.WriteStartDocument(true); 69
writer.WriteStartElement("methodCall"); 70
writer.WriteElementString("methodName", "pingback.ping"); 71
writer.WriteStartElement("params"); 72
73
writer.WriteStartElement("param"); 74
writer.WriteStartElement("value"); 75
writer.WriteElementString("string", sourceUrl.ToString()); 76
writer.WriteEndElement(); 77
writer.WriteEndElement(); 78
79
writer.WriteStartElement("param"); 80
writer.WriteStartElement("value"); 81
writer.WriteElementString("string", targetUrl.ToString()); 82
writer.WriteEndElement(); 83
writer.WriteEndElement(); 84
85
writer.WriteEndElement(); 86
writer.WriteEndElement(); 87
} 88
} 89
90
Events 117
118
} 119
} 120





