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;
}
}
原创粉丝点击