package com.cnit.action;
import java.io.IOException;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class HttpClientTest {
/**参数 登录参数*/
public static NameValuePair[] getValuePair(String UserName,String Password,String Chanllenge){
String md5 = UserName+Password+Chanllenge;
String response = getMD5(md5.getBytes());
NameValuePair[] pairs = new NameValuePair[]{
new NameValuePair("Username", UserName),
new NameValuePair("Password", Password),
new NameValuePair("Challenge",Chanllenge),
new NameValuePair("Response",response)
};
return pairs;
}
//http://172.16.76.44/tgi/iocontrol.tgi
public static int sentRequest(String url,String Username,String Password, String insPort,String cmd){
//test
if("test".equals(insPort)) return 200;
System.out.println("发送电源控制信息 地址:"+url +" 设备端口:"+insPort +" 命令:"+ cmd);
long startTime = System.currentTimeMillis();
//"http://172.16.76.44/login.htm"
//构造HttpClient的实例http://localhost:9090/nfyy
HttpClient httpClient = new HttpClient();
//参数设置
httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,"Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2");//设置信息
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
//获取随机码
GetMethod getMethod = new GetMethod(url);
String Chanllenge = "";
try {
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
httpClient.executeMethod(getMethod);
String str = getMethod.getResponseBodyAsString();
String c = str.split("Challenge")[3];
Chanllenge =c.substring(9, 17);
System.out.println("获取随机码值为:" +Chanllenge);
} catch (Exception e1) {
System.out.println("获取随机码值出错!出错原因:"+e1.getMessage());
}
//登录操作 和发送 控制命令
//创建GET方法的实例
PostMethod postMethod = new PostMethod(url+"/tgi/login.tgi");
//url "http://172.16.76.44/tgi/iocontrol.tgi"
PostMethod postMethod2 = new PostMethod(url+ "/tgi/iocontrol.tgi");
postMethod.setRequestBody(getValuePair(Username, Password, Chanllenge));
int statusCode = 0;
int cmdCode = 0;
try {
// 执行getMethod ***********************************
statusCode = httpClient.executeMethod(postMethod);
if(statusCode == HttpStatus.SC_OK)
System.out.println("模拟登录成功........!");
else
System.out.println("模拟登录失败..........!");
//发送命令参数 *************************************
postMethod2.setRequestBody(controlParams(insPort,cmd));
cmdCode =httpClient.executeMethod(postMethod2);
System.out.println("发送电源控制信息成功!返回控制码为"+cmdCode);
if (cmdCode != HttpStatus.SC_OK) {
System.out.println("Method failed: "
+ postMethod.getStatusLine());
}
// // 读取内容
// byte[] responseBody = getMethod.getResponseBody();
// // 处理内容
// System.out.println(new String(responseBody));
}
catch (HttpException e) {
System.out.println("模拟登录出错!"+e.getMessage());
// 发生致命的异常,可能是协议不对或者返回的内容有问题
// System.out.println("Please check your provided http address!");
// e.printStackTrace();
}
catch (IOException e) {
System.out.println(" 发生网络异常!"+e.getMessage());
//发生网络异常
//e.printStackTrace();
}
finally {
// 释放连接
getMethod.releaseConnection();
postMethod.releaseConnection();
postMethod2.releaseConnection();
}
System.out.println("本次发送控制命令所有时间为:"+(System.currentTimeMillis() - startTime));
return cmdCode;
}
//控制参数
public static NameValuePair[] controlParams(String insPort,String cmd){
NameValuePair[] nameVlauePair = new NameValuePair[32];
//起动 第一个
nameVlauePair[0] = new NameValuePair("pw1Name","1");
if("P60".equals(insPort))
nameVlauePair[1] = new NameValuePair("P60", cmd);
else
nameVlauePair[1] = new NameValuePair("P60","no");
nameVlauePair[2] = new NameValuePair("P60"+"_TS","0");
nameVlauePair[3] = new NameValuePair("P60"+"_TC","Off");
//起动 第一个 P61
nameVlauePair[4] = new NameValuePair("pw2Name","2");
if("P61".equals(insPort))
nameVlauePair[5] = new NameValuePair("P61", cmd);
else
nameVlauePair[5] = new NameValuePair("P61","no");
nameVlauePair[6] = new NameValuePair("P61"+"_TS","0");
nameVlauePair[7] = new NameValuePair("P61"+"_TC","Off");
//起动 第一个 P62
nameVlauePair[8] = new NameValuePair("pw3Name","3");
if("P62".equals(insPort))
nameVlauePair[9] = new NameValuePair("P62", cmd);
else
nameVlauePair[9] = new NameValuePair("P62","no");
nameVlauePair[10] = new NameValuePair("P62"+"_TS","0");
nameVlauePair[11] = new NameValuePair("P62"+"_TC","Off");
//起动 第一个 P63
nameVlauePair[12] = new NameValuePair("pw4Name","4");
if("P63".equals(insPort))
nameVlauePair[13] = new NameValuePair("P63", cmd);
else
nameVlauePair[13] = new NameValuePair("P63","no");
nameVlauePair[14] = new NameValuePair("P63"+"_TS","0");
nameVlauePair[15] = new NameValuePair("P63"+"_TC","Off");
//起动 第P64
nameVlauePa