DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(getSaveReplyBBSPostsUrl(bbsPostsUrl));
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("sessionhash", sessionHash);
cookie.setPath("/");
cookie.setDomain("liba.com");
cookieStore.addCookie(cookie);
httpClient.setCookieStore(cookieStore);
MultipartEntity entity = new MultipartEntity();
entity.addPart("content", new StringBody(content, Charset.forName("UTF-8")));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 302) {
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
if (header.getName().equals("Location")) {
log.info("location is:#0", header.getValue());
if (header.getValue().contains("/reply_success")) {
log.info("reply bbs posts success, bbsPostsUrl:#0, content:#1", bbsPostsUrl, content);
return;
}
}
}
log.error("reply bbs posts error, bbsPostsUrl:#0, content:#1, sessionHash:#2", bbsPostsUrl, content, sessionHash);
}
log.error("reply bbs posts error, status code:#0", response.getStatusLine().getStatusCode());
HttpPost httpPost = new HttpPost(getSaveReplyBBSPostsUrl(bbsPostsUrl));
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("sessionhash", sessionHash);
cookie.setPath("/");
cookie.setDomain("liba.com");
cookieStore.addCookie(cookie);
httpClient.setCookieStore(cookieStore);
MultipartEntity entity = new MultipartEntity();
entity.addPart("content", new StringBody(content, Charset.forName("UTF-8")));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 302) {
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
if (header.getName().equals("Location")) {
log.info("location is:#0", header.getValue());
if (header.getValue().contains("/reply_success")) {
log.info("reply bbs posts success, bbsPostsUrl:#0, content:#1", bbsPostsUrl, content);
return;
}
}
}
log.error("reply bbs posts error, bbsPostsUrl:#0, content:#1, sessionHash:#2", bbsPostsUrl, content, sessionHash);
}
log.error("reply bbs posts error, status code:#0", response.getStatusLine().getStatusCode());
使用Java实现BBS回复功能
本文介绍了一种使用Java实现向指定BBS帖子发送回复的方法。通过设置Cookie并构造包含回复内容的实体,实现了对特定论坛帖子的成功回复。


被折叠的 条评论
为什么被折叠?



