public int getConfig(String configName) throws MOConfigException {
/*
* telnet prompt is based on sysName, making sure snmp opeded in order
* to get sysName
*/
// down load config file
//登陆服务器 采用的是POST
String url = "http://"+this._ip+"/setup.cgi";
NameValuePair[] data_login = { new NameValuePair("ID", "11"),
new NameValuePair("mtg", "0"),
new NameValuePair("haveCookie", "0"),
new NameValuePair("backID", "30"),
new NameValuePair("psw", this._mo.getHttpPass()) };
HTTPPostMethod upm = new HTTPPostMethod();
try {
String ss = upm.postMethod(url, data_login);
if (ss.indexOf("Another user is already logged on") != -1) {
//System.out.println("Another user is already logged on");
throw new MOConfigException("Failed to get configuration file.");
}
if (ss.indexOf("Wrong password.") != -1) {
//System.out.println("Wrong password.");
throw new MOConfigException("Failed to get configuration file.");
}
} catch (IOException e) {
e.printStackTrace();
}
//get方法下载文件
String urlget = "http://"+this._ip+"/txtcfg.cgi?config.txt";
HTTPGetMethod ugm = new HTTPGetMethod();
String ss = null;
try {
ss = ugm.getMethod(urlget);
} catch (IOException e) {
throw new MOConfigException("Failed to get configuration file.");
}
try {
String templateFilename = ApplicationEnv.PATH_CONFIG_DOWNLOAD
+ File.separator + configName;
FileWriter file = new FileWriter(templateFilename);
BufferedWriter buff = new BufferedWriter(file);
file.write(ss, 0, ss.length());
buff.close();
file.close();
} catch (IOException e) {
throw new MOConfigException("Failed to get configuration file.");
}
LogServer.getLogger().debug("G610Config::getConfig() - start. logout");
//退出
NameValuePair[] data_exit = { new NameValuePair("ID", "12"),
new NameValuePair("mtg", "0"),
new NameValuePair("haveCookie", "0"),
new NameValuePair("referer", "100"),
new NameValuePair("systmSecs", "118041551"),
new NameValuePair("systmZ", "25") };
try {
upm.httpParser(upm.postMethod(url, data_exit));
} catch (IOException e) {
throw new MOConfigException("Failed to get configuration file.");
}
return 0;
}
//post方法
public String postMethod(String url, NameValuePair[] data)
throws IOException {
PostMethod postMethod = new PostMethod(url);
// 将表单的值放入postMethod中
postMethod.setRequestBody(data);
// 执行postMethod
int statusCode = 0;
byte[] responseBody = null;
try {
statusCode = _httpClient.executeMethod(postMethod);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = postMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
return "G610_PAGE_WARNING:The page was redirected to:"
+ location;
} else {
return "G610_PAGE_WARNING:Location field value is null.";
}
} else {
// System.out.println(postMethod.getStatusLine());
InputStream is = postMethod.getResponseBodyAsStream();
if (is != null) {
long contentLength = postMethod.getResponseContentLength();
if ((contentLength != -1) && (contentLength > PAGE_MAX_LEN)) {
throw new HttpContentTooLargeException("Content-Length is "
+ contentLength, PAGE_MAX_LEN);
}
ByteArrayOutputStream rawdata = new ByteArrayOutputStream(
contentLength > 0 ? (int) contentLength : 4 * 1024);
byte[] buffer = new byte[2048];
int pos = 0;
int len;
do {
len = is.read(buffer, 0, Math.min(buffer.length,
PAGE_MAX_LEN - pos));
if (len == -1)
break;
rawdata.write(buffer, 0, len);
pos += len;
} while (pos < PAGE_MAX_LEN);
// check if there is even more data
if (pos == PAGE_MAX_LEN) {
if (is.read() != -1)
throw new HttpContentTooLargeException(
"Content-Length not known but larger than "
+ PAGE_MAX_LEN, PAGE_MAX_LEN);
}
responseBody = rawdata.toByteArray();
}
}
postMethod.releaseConnection();
return EncodingUtil.getString(responseBody, postMethod
.getResponseCharSet());
}
//get方法
public String getMethod(String url )
throws IOException {
GetMethod getMethod = new GetMethod(url);
int statusCode = 0;
byte[] responseBody = null;
try {
statusCode = _httpClient.executeMethod(getMethod);
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = getMethod.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
return "G610_PAGE_WARNING:The page was redirected to:"
+ location;
} else {
return "G610_PAGE_WARNING:Location field value is null.";
}
} else {
// System.out.println(getMethod.getStatusLine());
InputStream is = getMethod.getResponseBodyAsStream();
if (is != null) {
long contentLength = getMethod.getResponseContentLength();
if ((contentLength != -1) && (contentLength > PAGE_MAX_LEN)) {
throw new HttpContentTooLargeException("Content-Length is "
+ contentLength, PAGE_MAX_LEN);
}
ByteArrayOutputStream rawdata = new ByteArrayOutputStream(
contentLength > 0 ? (int) contentLength : 4 * 1024);
byte[] buffer = new byte[2048];
int pos = 0;
int len;
do {
len = is.read(buffer, 0, Math.min(buffer.length,
PAGE_MAX_LEN - pos));
if (len == -1)
break;
rawdata.write(buffer, 0, len);
pos += len;
} while (pos < PAGE_MAX_LEN);
// check if there is even more data
if (pos == PAGE_MAX_LEN) {
if (is.read() != -1)
throw new HttpContentTooLargeException(
"Content-Length not known but larger than "
+ PAGE_MAX_LEN, PAGE_MAX_LEN);
}
responseBody = rawdata.toByteArray();
}
}
getMethod.releaseConnection();
return EncodingUtil.getString(responseBody, getMethod
.getResponseCharSet());
}