HTTPCLIENT 下载文件 GET 先登录 下载 退出
来源:互联网 发布:淘宝游戏专营恢复了吗 编辑:程序博客网 时间:2024/05/17 09:20
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());
}
- HTTPCLIENT 下载文件 GET 先登录 下载 退出
- HTTpClient实现get方式下载
- HttpClient 4 文件下载
- httpclient下载文件
- 使用httpclient下载文件
- 通过 HttpClient 下载 文件
- HttpClient下载文件
- 通过 HttpClient 下载 文件
- Httpclient 实现文件下载
- 通过 HttpClient 下载 文件
- httpclient 上传、下载文件
- 通过 HttpClient 下载 文件
- httpclient 文件下载
- HttpClient下载文件
- httpclient 上传文件、下载文件
- 使用HttpClient的GET方式下载数据
- 利用HttpClient 4.1 下载文件
- HttpClient 4 实现文件下载
- 目标!
- 2006年100款最佳安全工具谱
- NET中统一的存储过程调用方法
- .net保存数据到XML文件中
- 写数据到的Annotation Featureclass里面
- HTTPCLIENT 下载文件 GET 先登录 下载 退出
- demo
- flex actionscript 和 javascript 通讯(1) actionscript 调用javascript的函数
- Tomcat 5集群中的SESSION复制一(图)
- MFC应用程序中指针的获取(1)
- 林语堂:我生之初尚无为
- label与Annotation有关的代码
- 在 ASP.NET 中执行 URL 重写
- 设置魔兽世界中tab键选取的范围