首先,鄙视一下搜狐微博API,比起哪个新浪的日本人:Yusuke Yamamoto的还差一截呀!
没有完整的包装微博的API做为一个完整的SDK,真是让人费脑筋让人做一个测试.
话入正题:
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import javax.activation.MimetypesFileTypeMap;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
public class SohuBlogger {
HttpClient client = new HttpClient();
String SERVER_UPDATE = "http://api.t.sohu.com/statuses/upload.xml";
String USERNAME = "heweiya@gmail.com";
String PASSWORD = "XXXXX";
public static void main(String[] args) {
SohuBlogger controller = new SohuBlogger();
try {
controller.postMessage("测试微博!!!,这次带图片");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void postMessage(String msg) throws HttpException, IOException {
boolean needAuthentication = true;
if (needAuthentication) {
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT,
AuthScope.ANY_REALM),
new UsernamePasswordCredentials(USERNAME, PASSWORD));
}
PostMethod method = new PostMethod(SERVER_UPDATE);
if (needAuthentication) {
method.setDoAuthentication(true);
}
msg = URLEncoder.encode(msg, "UTF-8");
File file = new File("c:\\tt.jpg");
StringPart sp = new StringPart("status", msg);
FilePart fp = new FilePart("pic", file.getName(), file,new MimetypesFileTypeMap().getContentType(file),
"UTF-8");
fp.setTransferEncoding("binary");
MultipartRequestEntity mrp= new MultipartRequestEntity(new Part[]{sp,fp}, method
.getParams());
method.setRequestEntity(mrp);
//执行postMethod
client.executeMethod(method);
System.out.println("post status: " + method.getStatusLine().toString());
System.out.println("post page content: "
+ method.getResponseBodyAsString());
method.releaseConnection();
}
}
为了增加一下鄙视的力量,大家看看新浪微博API如何做到上面的一切.
public void pubSinaWeibo(List<BlogDetail> blogs) throws IOException{
for (int i = 0; i < blogs.size(); i++) {
//说明一下,这是我自己数据库里的东西,希望大家传入自己要用的东西,比如String类型,就不用使用FOR循环了.哈哈,懒得动代码了...
BlogDetail blog = blogs.get(i);
BlogInfo info = blog.getInfo();
System.out.println("正在导出日志" + i + ":" + info.getTitle());
Weibo weibo = new Weibo();
/*
* 此处需要填写AccessToken的key和Secret,可以从OAuthUpdate的执行结果中拷贝过来
*/
weibo.setToken("#######",
"######");
Status status;
try {
String weiboText = "@家军在" + blog.getPubDateTime() + "发布了QQ日志:"
+ info.getTitle() + ",请查看图片!";
String msg = URLEncoder.encode(weiboText, "UTF-8");
File file = new File("c:\\tt.jpg");
status = weibo.uploadStatus(msg, file);
//这一句就够了!
Thread.sleep(1350); // 休息0.25秒,太快会不会有问题?服务器负担太大?
} catch (WeiboException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}