W zasadzie, to publikuje ten kod, żeby zebrać feedback i dowiedzieć się w jakim kierunku ewentualnie rozwijać poniższą klasę.
Jest to bardzo, bardzo prosta klasa służąca do umieszczenia wpisu na blogu opartym o mechanizmy XMLRPC – np. WordPress, BlogEngine.Net. Ten konkretny przykład testowałem dla WordPressa i działa bez zarzutu. Jest kilka zmiennych nadmiarowych – posłużą do dalszego rozwoju :).
Wsparcie, jak zawsze mile widziane :).
[code:c#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Web;
namespace WordPress_Updater
{
class blogUpdater
{
int postID;
private string login;
private string pass;
private string url;
public string title;
public string content;
private string postXML;
private string action;
private string dataToPost;
public string serverResponse;
public string categories;
private string xmlParams;
public blogUpdater(string _url, string _login, string _password)
{
this.login = _login;
this.pass = _password;
this.url = _url;
}
private string post2xml(string _title, string _categories, string _content)
{
string postXML = "<title>" + _title + "</title>" +
"<category>" + _categories + "</category>" +
_content;
return postXML;
}
private void addXmlParam(string _type, string _value)
{
xmlParams += "<param>" +
" <value>";
if (_value != "") xmlParams += " <" + _type + ">" + _value + "</" + _type + ">";
else xmlParams += "<" + _type + "/>";
xmlParams += " </value>" +
" </param>";
}
public bool newPost()
{
this.action = "blogger.newPost";
xmlParams = string.Empty;
postXML = post2xml(this.title, this.categories, this.content);
postXML = HttpUtility.HtmlEncode(postXML);
this.dataToPost = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" +
"<methodCall>" +
"<methodName>" + action + "</methodName>" +
"<params>";
this.addXmlParam("string", "");
this.addXmlParam("string", "");
this.addXmlParam("string", this.login);
this.addXmlParam("string", this.pass);
this.addXmlParam("string", postXML);
this.addXmlParam("boolean", "1");
this.dataToPost += this.xmlParams;
this.dataToPost += "</params>" +
"</methodCall>";
return sendRequest();
}
private bool sendRequest()
{
WebRequest web = WebRequest.Create(this.url);
web.Method = "POST";
//web.ContentLength = this.dataToPost.Length;
web.ContentType = "text/xml";
Stream request = web.GetRequestStream();
StreamWriter sw = new StreamWriter(request);
sw.Write(this.dataToPost);
sw.Flush();
sw.Close();
request.Close();
WebResponse response = web.GetResponse();
Stream responseData = response.GetResponseStream();
StreamReader sr = new StreamReader(responseData);
this.serverResponse = sr.ReadToEnd();
sr.Close();
responseData.Close();
response.Close();
return true;
}
}
}
[/code]
Jak używać tego kodu?
[code:c#]
blogUpdater wp = new blogUpdater("http://PrzykladowyBlog***.wordpress.com/xmlrpc.php", login, haslo);
wp.content = “Treść wpisu”;
wp.title = “Tytuł wpisu”;
wp.categories = "0"; //do jakich kategorii chcemy go dodać – podajemy id po przecinku
wp.newPost();
//wp.serverResponse //odpowiedź XML serwera
[/code]
Currently rated 4.3 by 3 people
- Currently 4.333333/5 Stars.
- 1
- 2
- 3
- 4
- 5