日期:2014-05-20  浏览次数:21116 次

怎么导入这个包commons-lang.jar



本人用的是图上的eclipse版本在上图中,调试到现在都不能运行起来,代码是网上找的,



import java.io.IOException;
import java.sql.Date;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.sql.Date;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class web3linkApp {
public static void main(String[] args) {
while (true) {
try {
Thread.sleep(900000);//每900000毫秒顶贴一次v
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
web3linkApp.post();
}
}

public static void post() {
String url ="http://bbs.chd.sdo.com/TopicContent.aspx?act=SubmintTopicInfo&BoardID=2&TopicID=25479";
// "http://bbs.chd.sdo.com/TopicContent.aspx?BoardID=2&TopicID=70240";
//String url = "http://bbs.chd.sdo.com/TopicContent.aspx?act=SubmintTopicInfo&BoardID=2&TopicID=25479";
// String url3
// ="http://bbs.17173.com/rapid_reply.php?id=3256,3257,1910446";
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(url);
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));

HttpState initialState = new HttpState();
String cookies = "LHNGSRqV5fsRMbMPpkseDg==; al=yV6JL66nyLVV4HaTjsOKM0bCL4U7DYnv/IpAQqAcyQTg4hYiSA84IxGTEsFKMsk3OnDv7Ms12VQlzHftCjCcBw==";

initialState.addCookie(new Cookie("bbs.chd.sdo.com", "cu", cookies,
"/", new Date(2008, 6, 6), false));// 设置cookies

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
30000);
httpClient.setState(initialState);
httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);// RFC_2109是支持较普遍的一个,还有其他cookie协议

NameValuePair[] data = { new NameValuePair("subject", "up"), //顶贴内容标题:up 内容upupup
new NameValuePair("message", "upupup") };
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);

int statusCode = 0;
try {
statusCode = httpClient.executeMethod(postMethod);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
return;
}

postMethod.releaseConnection();// 释放连接

}

}


------解决方案--------------------
你缺的是 HttpClient 的包,不是commons-lang,去Apache开源网站上下载吧。