Hey,
I'm trying to create a program that lets you manage your own wordpress blog in multiple ways and shows you statistical data too and one of the functions that I was thinking about implementing is a function that of actually posts a comment on the blog but I'm having problems.

Here's the code that I'm using:

static void Main(string[] args)
        {
            byte[] buffer = Encoding.ASCII.GetBytes("author=plato&email=platom82@hotmail.com&url=&comment=Please+delete+this&submit=Submit+Comment&comment_post_ID=");
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://www.therunescapeblog.com/2010/03/analysis-of-rare-prices/wp-comments-post.php?");
            WebReq.Method = "POST";
            WebReq.KeepAlive = true;
            WebReq.ContentType = "application/x-www-form-urlencoded";
            WebReq.ContentLength = buffer.Length;

            Stream PostData = WebReq.GetRequestStream();
            PostData.Write(buffer, 0, buffer.Length);
            PostData.Close();

            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);

            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            Console.WriteLine(_Answer.ReadToEnd());

            Console.ReadLine();
        }

As you can see from the headers I'm trying to post a comment on this page: www.therunescapeblog.com/2010/03/analysis-of-rare-prices/

By the author "plato" whose email is "platom82@hotmail.com" who wants to say "Please delete this", however I'm encountering a few problems.

I doubt that the headers are incorrect as I've obtained them from an http sniffer. The uri is probably incorrect however I've tried changing the uri and headers for thousands of combinations but never actually managed to post a comment via HttpWebRequest.

I've never been able to understand HttpWebRequest/HttpWebResponses anyway so I've decided to seek some professional help.

Does anyone know what I'm doing wrong?

Thanks a lot!

Recommended Answers

All 6 Replies

If you have a fully formed URI, you will not need to call it via POST, you can use GET or just let it default.

You can also use a WebClient to do what you're attempting.
Here are two simple methods of calling to a "cgi" (for lack of a better term) that gives a response based on input:

using System;
using System.IO;
using System.Net;

namespace ConsoleApplication3
{
   class Program
   {
      static void Main(string[] args)
      {
         UseWebRequest();
         UseWebClient();
      }

      private static void UseWebRequest()
      {
         HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(
            "http://www.altavista.com/web/results?"
               + "itag=ody&q=WebRequest&kgs=1&kls=0"
         );
         WebResponse webResp = webReq.GetResponse();
         StreamReader fileIn = new StreamReader(webResp.GetResponseStream());
         Console.WriteLine(fileIn.ReadToEnd());
         fileIn.Close();
      }

      private static void UseWebClient()
      {
         WebClient wc = new WebClient();
         StreamReader fileIn = new StreamReader(
            wc.OpenRead("http://www.altavista.com/web/results?"
                  + "itag=ody&q=WebRequest&kgs=1&kls=0"));

         Console.WriteLine(fileIn.ReadToEnd());
         fileIn.Close();
      }
   }
}
commented: Cheers! +0

I c, before I actually used to use a WebClient since I found them much easier to work with, especially when having constructed a full working uri.

However this time the problem seems to be with the uri itself as in I can't get to construct a working uri without getting an error 404/405 or it doing nothing.

The http headers should be correct as I've checked them via HTTP sniffing (HTTPLook) and checked the form html source to see what needs to be passed.

I've honestly never had such a mysterious problem; I really don't know what I'm doing wrong when constructing the uri.

Anyway, Thanks a lot for your insight thines01!!!

I understand:
The IDs I noticed in the form are:
author=thisthat&
email=this@that.com&
url=&
comment=asdfgh&
comment_post_ID=207&
comment_parent=0&
smilies_toolbar=&

There is so much content in that HTML, there might be something additional going on that is preventing you (us or any bots) from doing the same thing.

...then again, it could just be the POST.

Hmmm I c.
I thought there might be something else going on thats preventing me from posting but my Html knowledge is really shabby so I couldn't conclude anything :/.

commented: thank you for posting this +0

I was searching the internet about the same exact thing, came across this thread and also have similar problems.

At first I approached the problem exactly like this even though I have this kind of programming (web stuff).

I've tried looking into some APIs such as the XML-RPC blogger APIs but there's nothing useful in there. Apparently there's a method called newPost which at a glance implies that you can create a post with however it's for webblog admins to post on their own blog only an not others.

Anyhow, from your last post I presume that you have already solved the problem so I sent you a message asking for a possible solution or you can post it here.

I was searching the internet about the same exact thing, came across this thread and also have similar problems.

At first I approached the problem exactly like this even though I have this kind of programming (web stuff).

I've tried looking into some APIs such as the XML-RPC blogger APIs but there's nothing useful in there. Apparently there's a method called newPost which at a glance implies that you can create a post with however it's for webblog admins to post on their own blog only an not others.

Anyhow, from your last post I presume that you have already solved the problem so I sent you a message asking for a possible solution or you can post it here.

No, sorry; creating such a function is still a mystery to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.