package net.dinguo.oschina; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.CookieStore; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.cookie.Cookie; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; public class Csdn { public static void main(String[] args) throws Exception { Csdn c = new Csdn(); CookieStore cookies = c.login("usernaem", "password"); String title = "测试登陆发帖子123"; String content = "测试发送登陆后的帖子321"; String tags = "csdn login"; c.publishBlog(cookies, title, content, tags); } public CookieStore login(String username, String password) throws Exception { DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("https://passport.csdn.net/ajax/accounthandler.ashx"); post.setHeader("Accept", "*/*"); post.setHeader("Accept-Encoding", "gzip, deflate"); post.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); post.setHeader("Host", "passport.csdn.net"); post.setHeader("Referer", "https://passport.csdn.net/account/login"); post.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0"); post.setHeader("X-Requested-With", "XMLHttpRequest"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("u", username)); nvps.add(new BasicNameValuePair("p", password)); nvps.add(new BasicNameValuePair("t", "log")); nvps.add(new BasicNameValuePair("remember", "0")); nvps.add(new BasicNameValuePair("f", "http%3A%2F%2Fwww.csdn.net%2F")); nvps.add(new BasicNameValuePair("rand", "0.4835865827484527")); post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); client.execute(post); CookieStore cs = client.getCookieStore(); List<Cookie> cookies = cs.getCookies(); // for (Cookie cookie : cookies) { // System.out.println(cookie.getName() + "\t" + cookie.getValue() // + "\t" + cookie.getDomain()); // } // // HttpEntity entity = resp.getEntity(); // String string = EntityUtils.toString(entity); // // System.out.println(string); return cs; } public void publishBlog(CookieStore cookies, String title, String content, String tags) throws Exception { String url = "http://write.blog.csdn.net/postedit?edit=1&isPub=1"; DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); post.addHeader("Accept", "*/*"); post.addHeader("Accept-Encoding", "gzip, deflate"); post.addHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); post.addHeader("Connection", "keep-alive"); post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); post.addHeader("Host", "write.blog.csdn.net"); post.addHeader("Pragma", "no-cache"); post.addHeader("Referer", "http://write.blog.csdn.net/postedit"); post.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0"); post.addHeader("X-Requested-With", "XMLHttpRequest"); client.setCookieStore(cookies); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("tags", tags)); nvps.add(new BasicNameValuePair("titl", title)); nvps.add(new BasicNameValuePair("typ", "1")); nvps.add(new BasicNameValuePair("cont", content)); nvps.add(new BasicNameValuePair("desc", "")); nvps.add(new BasicNameValuePair("flnm", "")); nvps.add(new BasicNameValuePair("chnl", "0")); nvps.add(new BasicNameValuePair("comm", "2")); nvps.add(new BasicNameValuePair("level", "0")); nvps.add(new BasicNameValuePair("tag2", "")); nvps.add(new BasicNameValuePair("artid", "0")); nvps.add(new BasicNameValuePair("stat", "publish")); post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpResponse resp = client.execute(post); HttpEntity entity = resp.getEntity(); String string = EntityUtils.toString(entity); System.out.println(string); } }