自动ping博客服务程序

几个月前写了一个博客自动ping程序,其实也没有什么技术含量,只是不想很快被遗忘,在此一记,希望各路好友,批评批正。

ping程序的,用处是把发布文章主动的提交搜索引擎,以便达到让搜索引擎快速收录。

程序原理主要通过RPC协议来实现的,下面简单学习一下RPC协议。

RPC协议

 

  英文原义:Remote Procedure Call Protocol

 

  中文释义:(RFC-1831)远程调用协议

 

  注解:一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。

 

  RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。在OSI网络通信模型中,RPC跨越了传输层和应用层。RPC使得开发包括网络分布式多程序在内的应用程序更加容易。

 

  RPC采用客户机/服务器模式。请求程序就是一个客户机,而服务提供程序就是一个服务器。首先,调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息的到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用过程接收答复信息,获得进程结果,然后调用执行继续进行。

 

程序主界面载图

废话少说了,看代码。ping程序内部核心代码都在这里了。

 

using System;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;

namespace PingService
{
    
public class PingBlog
    {
        
private string blogName = string.Empty;
        
private string blogUrl = string.Empty;
        
private string rssUrl = string.Empty;
        
private string updateUrl = string.Empty;
        
private string metaString = string.Empty;

        
public PingBlog(string name, string url, string fredUrl, string updateUrl, string tag)
        {
            blogName 
= name;
            blogUrl 
= url;
            rssUrl 
= fredUrl;
            
this.updateUrl = updateUrl;
            metaString 
= tag;
        }
//传参数传递需要ping目标 URL
        public string StartPing(string pingURL)
        {
            StreamReader sr 
= null;
            StringBuilder sb 
= new StringBuilder();
            
string strOutput = string.Empty;
            
string strXML = string.Empty;
            
try
            {
                strXML 
= BuildRCXML();
                
int len = strXML.Length;
                ASCIIEncoding ascii 
= new ASCIIEncoding();
                HttpWebRequest webReq 
= (HttpWebRequest)WebRequest.Create(pingURL);
                webReq.Method 
= "POST";
                webReq.Timeout 
= 3000;//超时为3秒
           webReq.ContentType = "text/xml";
                webReq.Credentials 
= CredentialCache.DefaultNetworkCredentials;
                webReq.ContentLength 
= len;

                Stream streamRequest 
= webReq.GetRequestStream();
                
byte[] bt = ascii.GetBytes(strXML);
                streamRequest.Write(bt, 
0, len);
                HttpWebResponse webRes 
= (HttpWebResponse)webReq.GetResponse();

                sr 
= new StreamReader(webRes.GetResponseStream(), Encoding.ASCII);
                
string ret = sr.ReadToEnd();
                sb.AppendLine(
"\n");
                sb.AppendLine(ret);

                
if (ret.IndexOf("<boolean>0</boolean>")>=0)
                {
                    sb.AppendLine(
"\n\t Ping服务提交成功!");
                }
                
else if (ret.IndexOf("<boolean>1</boolean>">= 0)
                {
                    sb.AppendLine(
"\n\t Ping服务提交失败!");
                }
              
if (sr != null)
                {
                    sr.Close();
                   sr
=null;
                }
            }
            
catch (Exception ex)
            {
                sb.AppendLine(ex.Message);
                sb.AppendLine(ex.StackTrace);
            }
            
finally
            {
                strOutput 
= sb.ToString();
                
if(sr!=null)
                {
                    sr.Dispose();
                }
            }
            
return strOutput;
        }

//根据RPC协议API文档构造XML
      private string BuildRCXML()
        {
            System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
            sb.Append(
"<?xml version=\"1.0\"?>");
            sb.Append(
"<methodCall>");
            sb.Append(
"<methodName>weblogUpdates.extendedPing</methodName>");//weblogUpdates.ping
            sb.Append("<params>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", blogName);
            sb.Append(
"</param>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", blogUrl);
            sb.Append(
"</param>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", updateUrl);
            sb.Append(
"</param>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", rssUrl);
            sb.Append(
"</param>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", metaString);
            sb.Append(
"</param>");

            sb.Append(
"</params>");
            sb.Append(
"</methodCall>");
            
return sb.ToString();

            }
    }
}

复制代码
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;

namespace PingService
{
    
public class PingBlog
    {
        
private string blogName = string.Empty;
        
private string blogUrl = string.Empty;
        
private string rssUrl = string.Empty;
        
private string updateUrl = string.Empty;
        
private string metaString = string.Empty;

        
public PingBlog(string name, string url, string fredUrl, string updateUrl, string tag)
        {
            blogName 
= name;
            blogUrl 
= url;
            rssUrl 
= fredUrl;
            
this.updateUrl = updateUrl;
            metaString 
= tag;
        }
//传参数传递需要ping目标 URL
        public string StartPing(string pingURL)
        {
            StreamReader sr 
= null;
            StringBuilder sb 
= new StringBuilder();
            
string strOutput = string.Empty;
            
string strXML = string.Empty;
            
try
            {
                strXML 
= BuildRCXML();
                
int len = strXML.Length;
                ASCIIEncoding ascii 
= new ASCIIEncoding();
                HttpWebRequest webReq 
= (HttpWebRequest)WebRequest.Create(pingURL);
                webReq.Method 
= "POST";
                webReq.Timeout 
= 3000;//超时为3秒
           webReq.ContentType = "text/xml";
                webReq.Credentials 
= CredentialCache.DefaultNetworkCredentials;
                webReq.ContentLength 
= len;

                Stream streamRequest 
= webReq.GetRequestStream();
                
byte[] bt = ascii.GetBytes(strXML);
                streamRequest.Write(bt, 
0, len);
                HttpWebResponse webRes 
= (HttpWebResponse)webReq.GetResponse();

                sr 
= new StreamReader(webRes.GetResponseStream(), Encoding.ASCII);
                
string ret = sr.ReadToEnd();
                sb.AppendLine(
"\n");
                sb.AppendLine(ret);

                
if (ret.IndexOf("<boolean>0</boolean>")>=0)
                {
                    sb.AppendLine(
"\n\t Ping服务提交成功!");
                }
                
else if (ret.IndexOf("<boolean>1</boolean>">= 0)
                {
                    sb.AppendLine(
"\n\t Ping服务提交失败!");
                }
              
if (sr != null)
                {
                    sr.Close();
                   sr
=null;
                }
            }
            
catch (Exception ex)
            {
                sb.AppendLine(ex.Message);
                sb.AppendLine(ex.StackTrace);
            }
            
finally
            {
                strOutput 
= sb.ToString();
                
if(sr!=null)
                {
                    sr.Dispose();
                }
            }
            
return strOutput;
        }

//根据RPC协议API文档构造XML
      private string BuildRCXML()
        {
            System.Text.StringBuilder sb 
= new System.Text.StringBuilder();
            sb.Append(
"<?xml version=\"1.0\"?>");
            sb.Append(
"<methodCall>");
            sb.Append(
"<methodName>weblogUpdates.extendedPing</methodName>");//weblogUpdates.ping
            sb.Append("<params>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", blogName);
            sb.Append(
"</param>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", blogUrl);
            sb.Append(
"</param>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", updateUrl);
            sb.Append(
"</param>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", rssUrl);
            sb.Append(
"</param>");

            sb.Append(
"<param>");
            sb.AppendFormat(
"<value>{0}</value>", metaString);
            sb.Append(
"</param>");

            sb.Append(
"</params>");
            sb.Append(
"</methodCall>");
            
return sb.ToString();

            }
    }
}
复制代码

 

整个源码包下载点击此处!转载请保留原文地址
 
http://www.divmy.com/
 
 
http://www.cnblogs.com/cgli/archive/2011/04/01/2001958.html
posted @ 2013-03-18 14:30  左正  阅读(290)  评论(0编辑  收藏  举报