编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

java实现上网登录

wxchong 2024-06-24 19:34:37 开源技术 64 ℃ 0 评论

下面是自动输入用户名和密码,登录web的代码:

package Test;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;

import org.apache.commons.httpclient.Header;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.httpclient.UsernamePasswordCredentials;

import org.apache.commons.httpclient.auth.AuthScope;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClientUse {

public static void main(String[] args) throws HttpException, IOException {

HttpClient httpClient = new HttpClient();

//需要验证

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("username", "password");

httpClient.getState().setCredentials(AuthScope.ANY, creds);

//设置http头

List <Header> headers = new ArrayList <Header>();

headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));

httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);

// GetMethod method = new GetMethod("http://localhost:15672/api/exchanges/%2F/amq.direct");

PostMethod method = new PostMethod( "http://192.3.0.13/login" );

method.setDoAuthentication(true);

method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,

new DefaultHttpMethodRetryHandler(3, false));

try {

int statusCode = httpClient.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {

System.out.println("Method failed code="+statusCode+": " + method.getStatusLine());

} else {

System.out.println(new String(method.getResponseBody(), "utf-8"));

}

} finally {

method.releaseConnection();

}

}

}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表