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

希望大家给点提示,先谢谢了!
我要用程序登陆到一个网站(知道用户名和密码);然后把固定页面的数据下载下来,我该怎么办,除了用httpunit以外还有其他的办法吗?

------解决方案--------------------
1.直接FTP到 网站的空间,把否个目录下的某个文件下载下来就行了
2.URLCONNECTION和HTTP交互,得到数据生成文件
3.SOCKET +URL,下载某些文件
------解决方案--------------------
如果你不是为了练习,你可以先登录,然后用SuperHTTP下载。

它可以保持原有的存放顺序。
------解决方案--------------------
用HttpClient就可以了呀~

public static void testClient(){
HttpClient client = new HttpClient();
PostMethod method = new PostMethod( "你的url ");
Part[] parts = { new StringPart( "logname ", "aaa "), new StringPart( "logpwd ", "bbb ") };
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
try{
int status = client.executeMethod(method);
if (status == HttpStatus.SC_OK){
String resource = method.getResponseBodyAsString();
System.out.println(resource);
}else{
System.out.println( "读取网页失败Code=[ " + status + "]! ");
}
}catch(Exception e){
e.printStackTrace();
}
}
其中:
Part[] parts = { new StringPart( "logname ", "aaa "), new StringPart( "logpwd ", "bbb ") };
是存放你的用户帐号和密码的,具体参数名你要从网页上找;如果网页上还有其他参数,也一同加进来,不然很可能登陆失败~


注意:用commons-httpclient-3.0.1.jar的时候,另外4个也少不了commons-logging-adapters-1.1.jar,commons-logging-api-1.1.jar,commons-logging-1.1.jar,commons-codec-1.3.jar,apache官方网站都有下,用最新版就可以了。