Archive

Posts Tagged ‘code’

Free C# Class – iliPing – Threadable, Fast, No External Classes Required

August 23rd, 2010 No comments

Before I give you another free C# class, I thought I may pass along a bit of a ProTip: it seems to me that the easiest money I make is from TextLinkAds. TLA capitalizes on the fact that spiders at least visit your site, even if real humans don’t. Join and give it a try.

This is a good class, and works well if you thread it. It works without using any other XML-RPC classes externally, and the only site it doesn’t work for me is sites that use nonstandard XML-RPC parameters like autopinger. I usually set it up to just go through a list of sites/blogs with their titles, and a list of XML-RPC ping service URI’s. I also thread this, and can ping quite quickly. This should be compatible with both .NET Framework, and my personal favorite, Mono (check out MonoDevelop sometime!).

Feel free to use this however and modify it…if you make any changes, please pay it forward and share them with everyone else so they can use the improvements too, since this is opensource.


/* iliPing - (C)2010 by ilikenwf, http://www.ilikenwf.com
* Feel free to use and/or modify this for any purpose...
* Please pay it forward and share any improvements you make
* Based on the XML-RPC pinging in C# class from
* http://mboffin.com/post.aspx?id=1613 modified and broken up */


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Net;

namespace pinger
{
///

/// Sends an XML-RPC ping for the given blog, blog url, and ping service url.
///

public class pingRequestor : IDisposable
{
public string pingRequest(string blogname, string blogurl, string pingurl, bool verbosity,
int httpTO) {

string output = string.Empty;
try {
HttpWebRequest webreqPing = (HttpWebRequest)WebRequest.Create(pingurl);
webreqPing.UserAgent = "WordPress 3.0.1";
webreqPing.Timeout = httpTO;
webreqPing.Method = "POST";
webreqPing.ContentType = "text/xml";

// Get the stream for the web request
Stream streamPingRequest = (Stream)webreqPing.GetRequestStream();

// Create an XML text writer that writes to the web request's stream
XmlTextWriter xmlPing = new XmlTextWriter(streamPingRequest, Encoding.UTF8);

// Build the ping, using the BlogName and BlogUrl
xmlPing.WriteStartDocument();
xmlPing.WriteStartElement("methodCall");
xmlPing.WriteElementString("methodName", "weblogUpdates.ping");
xmlPing.WriteStartElement("params");
xmlPing.WriteStartElement("param");
xmlPing.WriteElementString("value", blogname);
xmlPing.WriteEndElement();
xmlPing.WriteStartElement("param");
xmlPing.WriteElementString("value", blogurl);
xmlPing.WriteEndElement();
xmlPing.WriteEndElement();
xmlPing.WriteEndElement();

// Close the XML text writer, flusing the XML to the stream
xmlPing.Close();

// Send the request and store the response, then get the response's stream
HttpWebResponse webrespPing = (HttpWebResponse)webreqPing.GetResponse();

StreamReader streamPingResponse = new StreamReader(
webrespPing.GetResponseStream());
XmlDocument response = new XmlDocument();

// Store the result in an XmlDocument for parsing if verbosity is off
if (verbosity == false) {
response.LoadXml(streamPingResponse.ReadToEnd());
} else {
output = streamPingResponse.ReadToEnd();
}

// Close the response stream and the response itself
streamPingResponse.Close();
webrespPing.Close();

if (verbosity == false)
{
// Check the response to determine success or failure
XmlElement flerror = (XmlElement)response.SelectSingleNode("//boolean");
switch (flerror.InnerText)
{
case "0":
output = "[Success] ";
break;
case "false":
output = "[Success] ";
break;
default:
output = "[Failure] ";
break;
}
}
} catch {
// Timeout here is somewhat generic and nonspecific
output = "[Timeout] ";
}
// Return the result
return output;
}

public void Dispose() {
}

}
}