package drcom;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class Login {
String userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727; MAXTHON 2.0)";
HttpClient client = new HttpClient();
static final String TARGET_SITE = "10.29.0.250";
static final int TARGET_PORT = 80;
public static void main(String[] args) throws HttpException, IOException {
Login l=new Login();
PostMethod method=new PostMethod("http://10.29.0.250/F.htm");
l.initMethod(method);
l.fillLogonData(method);
method.setRequestHeader("Referer","http://10.29.0.250/");
l.client.executeMethod(method);
String s=l.getResponse(method);
System.out.println(s);
}
public Login() {
client.getHostConfiguration().setHost(TARGET_SITE, TARGET_PORT);
}
public void initMethod(HttpMethod method) {
method.setRequestHeader("User-Agent", userAgent);
method.setRequestHeader("Accept-Encoding", "gzip, deflate");
method.setRequestHeader("Accept-Language", "zh-cn");
method.setRequestHeader("Accept", "*/*");
method.setRequestHeader("Connection", "Keep-Alive");
method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
}
public String getResponse(HttpMethod method) throws IOException {
InputStream is = method.getResponseBodyAsStream();
StringBuffer buf = new StringBuffer();
byte b[] = new byte[16384];
int len = 0;
while ((len = is.read(b)) > 0) {
buf.append(new String(b, 0, len, "gb2312"));
}
String response = buf.toString();
return response;
}
public void fillLogonData(PostMethod method) {
NameValuePair[] data = new NameValuePair[3];
data[0] = new NameValuePair("DDDDD", "username");
data[1] = new NameValuePair("upass", "password");
data[2]=new NameValuePair("0MKKey","登录 Login");
method.addParameters(data);
}
}
没有评论:
发表评论