如果网站登录才能查看信息,需要用程序先登录才能下载页面,下面的程序在有些网站是可以用的,但是改用于人的经济论坛不行,表单提交处的问题。请各位大虾帮忙看看,表单提交设置有什么问题。多谢import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;public class Renda { public static void main(String args[]) {
String pageUrl = "http://www.pinggu.org/bbs/space.php?uid=577698";
String dir = "D:/";
String user = "XXX";
String password = "XXX"; // PostMethod post = initPost("https://twitter.com/sessions");
PostMethod post = initPost("http://www.pinggu.org/bbs/logging.php?action=login&loginsubmit=yes");

// PostMethod post = initPost("http://www.pinggu.org/bbs/logging.php?action=login");

HttpClient client = initClient();
try {
login(post, client, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PostMethod post2 = initPost(pageUrl);
download(pageUrl, dir, client, post2);
} public static PostMethod initPost(String url) {
NameValuePair[] nvp = {
new NameValuePair(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1"),
new NameValuePair("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
new NameValuePair("Accept-Language",
"zh-cn,en-us;q=0.7,zh;q=0.3"),
new NameValuePair("Accept-Charset",
"gb2312,utf-8;q=0.7,*;q=0.7") };
PostMethod post = new PostMethod(url);
post.setRequestBody(nvp);
return post;
} public static HttpClient initClient() {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost("twitter.com");
client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
return client;
} public static void login(PostMethod post, HttpClient client, String email,
String password) throws Exception {

post.removeParameter("formhash");
post.addParameter("formhash", "8701cabb"); post.removeParameter("loginfield");
post.addParameter("loginfield", "hidden");

post.removeParameter("username");
post.addParameter("username", email);

post.removeParameter("password");
post.addParameter("password", password);

// post.removeParameter("password3");
// post.addParameter("password3", password);

post.removeParameter("questionid");
post.addParameter("questionid", "0");

post.removeParameter("answer");
post.addParameter("answer", ""); post.removeParameter("cookietime");
post.addParameter("cookietime", "");

client.executeMethod(post);
post.releaseConnection();
int statuscode = post.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
|| (statuscode == HttpStatus.SC_SEE_OTHER)
|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
// 读取新的URL地址
Header header = post.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals("")))
newuri = "/";
GetMethod redirect = new GetMethod(newuri);
client.executeMethod(post);
redirect.releaseConnection();
}
}
} public static void download(String pageUrl, String dir, HttpClient client,
PostMethod post) {
try {
// PostMethod post = initPost(pageUrl);
System.out.println(client.executeMethod(post));
// post.releaseConnection(); int statuscode = post.getStatusCode();
GetMethod redirect = null;
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
|| (statuscode == HttpStatus.SC_SEE_OTHER)
|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
// 读取新的URL地址
Header header = post.getResponseHeader("location");
if (header != null) {
System.out.println("header is not null");
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals("")))
newuri = "/";
redirect = new GetMethod(newuri);
System.out.println(client.executeMethod(redirect));
// redirect.releaseConnection();
}
} InputStream in = null;
if (redirect == null) {
in = post.getResponseBodyAsStream();
System.out.println("redirect is null");
} else {
in = redirect.getResponseBodyAsStream();
System.out.println("redirect is not null");
} System.out.println(in);
InputStreamReader isr = new InputStreamReader(in, "GBK");
BufferedReader br = new BufferedReader(isr); File fp = new File(dir);
if (!fp.exists()) {
if (!fp.mkdirs())
System.out.println(dir + "\tcreate fail");
}
String file = "2.html";
File fc = new File(fp, file);
fc.createNewFile(); Writer writer = new OutputStreamWriter(new FileOutputStream(dir
+ file), "GBK");
String str;
while ((str = br.readLine()) != null) {
writer.write(str);
writer.write('\n');
}
writer.close();
in.close();
br.close();
System.out.println(dir + file);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}