shoop (mcfnord) wrote in lj_dev,
shoop
mcfnord
lj_dev

A C# LINQish RPC call to LJ

I put together some C# XDocument code to call LJ via RPC.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Net;
using System.IO;

namespace SendMessageTOLJ
{
  class SendIt
  {
    static void Main(string[] args)
    {
      while (true)
      {
        XDocument xd = new XDocument();
        xd.Add(new XElement("methodCall",
              new XElement("methodName", "LJ.XMLRPC.sendmessage"),
                new XElement("params",
                  new XElement("param",
                    new XElement("value",
                      new XElement("struct",
                        new XElement("member",
                          new XElement("name", "username"),
                          new XElement("value", new XElement("string", "mcfnord"))),
                        new XElement("member",
                          new XElement("name", "password"),
                          new XElement("value", new XElement("string", "mypass"))),
                        new XElement("member",
                          new XElement("name", "subject"),
                          new XElement("value", new XElement("string", "whatever"))),
                        new XElement("member",
                          new XElement("name", "body"),
                          new XElement("value", new XElement("string", "http://ljmindmap.com/"))),
                        new XElement("member",
                          new XElement("name", "to"),
                          new XElement("value", new XElement("string", "brad")))))))));

        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] data = encoding.GetBytes("" + xd.ToString());
        xd = null;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.livejournal.com/interface/xmlrpc");
        request.UserAgent = "http://ljmindmap.com/; livejournal.mindmap.jrd@xoxy.net";
        request.Method = "POST";
        request.ContentType = "text/xml"; //  "application/x-www-form-urlencoded";
        request.ContentLength = data.GetLength(0);

        Stream newStream = request.GetRequestStream();
        newStream.Write(data, 0, data.GetLength(0));
        newStream.Close();

        WebResponse response = null;
        response = request.GetResponse();
        Stream s = response.GetResponseStream();
        StreamReader sr = new StreamReader(s);
        string fd = sr.ReadToEnd();
        Console.WriteLine(fd);
        System.Diagnostics.Debug.Assert(false == fd.Contains("ault"));
      }
    }
  }
}


Tags: client, client: c#, client: xmlrpc
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments