校内网博客自动发布机器人java代码

没大有时间写注释,主要是懒,大家将就看吧。

自己写的校内网博客发布机器人java代码
自动在校内网博客发布文章
这是一个很简单的用于说明性质的代码,没有界面,你可以对它进行改进,例如从数据库读取文章内容等,以实现更强大的功能。

 

 

 

package http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;

public final class XiaoNeiBlog {
    public static void postBlog()
    {
        HttpClient client=new HttpClient();
        PostMethod post=new UTF8PostMethod("http://login.xiaonei.com/Login.do");
        post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        NameValuePair[] data=
        {
            new NameValuePair("email","[email protected]"),
            new NameValuePair("password","12345678")
        };
        post.setRequestBody(data);
        try {
            client.executeMethod(post);
            Cookie [] cookies=client.getState().getCookies();
            InputStream stream=post.getResponseBodyAsStream();
            writeStream(stream);
            stream.close();
            post.releaseConnection();         
            post=new UTF8PostMethod("http://blog.xiaonei.com/NewEntry.do");
              post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            data=new NameValuePair[]{
                new NameValuePair("title","这是怎么一回子事呢??")  ,
                new NameValuePair("body","调试了半天程序,终于成功了。就把这些内容写到博客里了。")  ,
                new NameValuePair("categoryId","0")  ,
                new NameValuePair("blogControl","99")  ,
                new NameValuePair("passwordProtedted","0")
            };
            post.setRequestBody(data);
            client.executeMethod(post);
            stream=post.getResponseBodyAsStream();
            writeStream(stream);
            stream.close();
            post.releaseConnection();
        }catch (HttpException ex) {
            Logger.getLogger(XiaoNeiBlog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(XiaoNeiBlog.class.getName()).log(Level.SEVERE, null, ex);
        }
       
    }
    private static void writeStream(InputStream stream) throws IOException
    {
        BufferedReader reader=new BufferedReader(new InputStreamReader(stream));
        String line=null;
        for(line=reader.readLine();line!=null;line=reader.readLine())
        {
            System.out.println(line);
        }
        reader.close();
    }


}

你可能感兴趣的:(java)