欢迎大家来到IT世界,在知识的湖畔探索吧!
Java访问网络,模拟登陆,进入网站内部(使用HttpClient), jar包在下面可以直接下载
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class Link {
static final String LOGON_SITE = “localhost” ;//主机地址
static final int LOGON_PORT = 18181;//端口号
public static void main(String[] args) throws Exception{
HttpClient client = new HttpClient();//打开新窗口
client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);//配置
// 模拟手动登录页面,home是处理登陆的antion
PostMethod post = new PostMethod( “/home” );
NameValuePair username = new NameValuePair( “username” , “admin” );
NameValuePair pass = new NameValuePair( “password” , “admin” );
post.setRequestBody( new NameValuePair[]{username,pass});
int status = client.executeMethod(post);
post.releaseConnection();
// 查看 cookie,并传递Cookies到服务器认证
CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, “/” , false , client.getState().getCookies());
if (cookies.length == 0) {
System.out.println( “没有Cookies” );
} else {
for ( int i = 0; i < cookies.length; i++) {
System.out.println(“Cookies:”+cookies[i].toString());
}
}
// 因为前面已经登陆了,也加入了Cookies,所以才可以直接访问这个地址,这个才是需要访问的真正antion
GetMethod get=new GetMethod(“/corpcode/renzheng?Corpcode=CXXXXX&Code=1231”);
client.executeMethod(get);
System.out.println(“Get请求:”+get.getResponseBodyAsString());
get.releaseConnection();
}
}
jar包下载 lib :http://waitlove.cn/?p=143
如果你换这类文章,可以订阅我,我还会继续发类似文章的。谢谢你————vsalw
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/22193.html