httpclient代理请求框架 Result类
来源:互联网 发布:国家税务电子申报软件 编辑:程序博客网 时间:2024/05/20 14:40
package com.shuzhan.proxy;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.zip.GZIPInputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;
/**
* 结果封装类
*
* @author killfen
* @author killfen
* @version 1.0.0
*/
public class Result {
private String cookie; // 返回的cookie值
private int statusCode; // 请求网页返回状态代码如果是200,则请求页面成功。
private HashMap<String, String> headerAll;
private HttpEntity httpEntity;
private String content; // 返回数据内容
/**
* 读取返回的cookie值
*
* @return 字符串
*/
public String getCookie() {
return cookie;
}
/**
* 网站的访问状态
*
* @return 200为访问页面成功。详细请查阅http协议的材料
*/
public int getStatusCode() {
return statusCode;
}
/**
* 获取放回的头部信息
*
* @return 头部信息集合
*/
public HashMap<String, String> getHeaders() {
return headerAll;
}
public void setHeaders(Header[] headers) {
headerAll = new HashMap<String, String>();
for (Header header : headers) {
headerAll.put(header.getName(), header.getValue());
}
}
public HttpEntity getHttpEntity() {
return httpEntity;
}
public void setHttpEntity(HttpEntity httpEntity) {
this.httpEntity = httpEntity;
}
/**
* 返回页面内容
*
* @param charset
* 页面编码
* @return 字符串。如果异常返回null
*/
public String getContent(String charset) {
try {
return EntityUtils.toString(httpEntity, charset);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 如果页面是通过gzip压缩过的。可以使用该方法进行解压
*
* @param charSet
* 字符编码
* @return InputStreamReade对象;
*/
public String getContentForGzip(String charset) {
if (httpEntity.getContentEncoding().getValue().indexOf("gzip") > -1) {
try {
GZIPInputStream gzipis = new GZIPInputStream(httpEntity
.getContent());
InputStreamReader isr = new InputStreamReader(gzipis, charset); // 设置读取流的编码格式,自定义编码
java.io.BufferedReader br = new java.io.BufferedReader(isr);
String tempbf;
StringBuffer sb = new StringBuffer();
while ((tempbf = br.readLine()) != null) {
sb.append(tempbf);
sb.append("\r\n");
}
gzipis.close();
isr.close();
return sb.toString();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public void setCookie(String cookie) {
this.cookie = cookie;
}
public void setStatusCode(int statusCode2) {
this.statusCode = statusCode2;
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.zip.GZIPInputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;
/**
* 结果封装类
*
* @author killfen
* @author killfen
* @version 1.0.0
*/
public class Result {
private String cookie; // 返回的cookie值
private int statusCode; // 请求网页返回状态代码如果是200,则请求页面成功。
private HashMap<String, String> headerAll;
private HttpEntity httpEntity;
private String content; // 返回数据内容
/**
* 读取返回的cookie值
*
* @return 字符串
*/
public String getCookie() {
return cookie;
}
/**
* 网站的访问状态
*
* @return 200为访问页面成功。详细请查阅http协议的材料
*/
public int getStatusCode() {
return statusCode;
}
/**
* 获取放回的头部信息
*
* @return 头部信息集合
*/
public HashMap<String, String> getHeaders() {
return headerAll;
}
public void setHeaders(Header[] headers) {
headerAll = new HashMap<String, String>();
for (Header header : headers) {
headerAll.put(header.getName(), header.getValue());
}
}
public HttpEntity getHttpEntity() {
return httpEntity;
}
public void setHttpEntity(HttpEntity httpEntity) {
this.httpEntity = httpEntity;
}
/**
* 返回页面内容
*
* @param charset
* 页面编码
* @return 字符串。如果异常返回null
*/
public String getContent(String charset) {
try {
return EntityUtils.toString(httpEntity, charset);
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 如果页面是通过gzip压缩过的。可以使用该方法进行解压
*
* @param charSet
* 字符编码
* @return InputStreamReade对象;
*/
public String getContentForGzip(String charset) {
if (httpEntity.getContentEncoding().getValue().indexOf("gzip") > -1) {
try {
GZIPInputStream gzipis = new GZIPInputStream(httpEntity
.getContent());
InputStreamReader isr = new InputStreamReader(gzipis, charset); // 设置读取流的编码格式,自定义编码
java.io.BufferedReader br = new java.io.BufferedReader(isr);
String tempbf;
StringBuffer sb = new StringBuffer();
while ((tempbf = br.readLine()) != null) {
sb.append(tempbf);
sb.append("\r\n");
}
gzipis.close();
isr.close();
return sb.toString();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public void setCookie(String cookie) {
this.cookie = cookie;
}
public void setStatusCode(int statusCode2) {
this.statusCode = statusCode2;
}
}
- httpclient代理请求框架 Result类
- httpclient代理请求框架 Header类
- httpclient代理请求框架 Proxy类
- HttpClient通过代理进行请求
- httpClient 设置代理请求接口
- HttpClient设置代理的请求
- HttpClient通过代理进行请求
- HttpClient 设置代理 及Fiddler查看请求
- Android中的httpclient框架发送get请求
- httpClient通过代理(Http Proxy)进行请求
- httpClient通过代理(Http Proxy)进行请求
- HttpClient (HTTP 请求工具类)
- httpClient 代理
- 【末世旅行之Java】HttpClient工具类,包含POST和GET请求,使用了连接池和代理
- HttpClient请求
- HttpClient请求
- HttpClient请求
- httpClient 请求
- elance 兼职手册
- GDI+编程10个基本技巧
- 垃圾回收编程
- 各地DNS服务器地址 详细
- httpclient代理请求框架 Header类
- httpclient代理请求框架 Result类
- httpclient代理请求框架 Proxy类
- cgic 库 对makefile的回忆 以及gcc动态链接的复习
- 使用python解析百度搜索结果
- 谈DIV+CSS样式表命名的规则方法
- IE CSS Bug
- Android Camera拍照常见问题小结
- error while loading shared libraries: cannot restore segment prot after reloc: Permission denied错误原
- 贯穿你一生的14条社会生计