`
weimou66
  • 浏览: 1246857 次
文章分类
社区版块
存档分类
最新评论

有人有兴趣给搜狐做个BlackBerry版本围脖吗?使用搜狐微博API嘀咕嘀咕

 
阅读更多

API说明:http://open.t.sohu.com/

开发支持:openapi@sohu-inc.com

下面的例子程序,在笔记本上面调试通过。

如果要做黑莓版本,代码可能稍微复杂一点。但是原理一样,都是走HTTP协议POST微博数据到sohu。

依赖类库:commons-codec-1.3.jar,commons-httpclient-3.0.1.jar,commons-logging-1.1.jar

public class SohuBlogger {

HttpClient client = new HttpClient();
String SERVER_UPDATE = "http://api.t.sohu.com/statuses/update.xml";
String USERNAME =cafebabe@xxx.com;
String PASSWORD ="xxx";

public static void main(String[] args) {
SohuBlogger controller = new SohuBlogger();
try {
controller.postMessage("Hello world中文");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}//

public void postMessage(String msg) throws HttpException, IOException {
BASE64Encoder decoder = new BASE64Encoder();
//msg = decoder.encodeBuffer(msg.getBytes("UTF-8"));

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 UTF8PostMethod(SERVER_UPDATE);

if (needAuthentication) {
method.setDoAuthentication(true);
}
method.addParameter("status", msg);

client.executeMethod(method);

System.out.println("post status: " + method.getStatusLine().toString());
System.out.println("post page content: "
+ method.getResponseBodyAsString());

// release any connection resources used by the method
method.releaseConnection();
}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics