HTTPCLIENT POST 返回信息处理 避免返回数据过大
2007-06-06 10:51
591 查看
public final int PAGE_MAX_LEN = 1024 * 1024;
//调用
ss = getHTMLcontent(post.getResponseBodyAsStream(), post.getResponseContentLength(), post.getResponseCharSet());
//函数
public String getHTMLcontent(InputStream is,long contentLength,String charSet) throws IOException
{
byte[] responseBody = null;
if (is != null) {
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();
}
return EncodingUtil.getString(responseBody, charSet);
}
//调用
ss = getHTMLcontent(post.getResponseBodyAsStream(), post.getResponseContentLength(), post.getResponseCharSet());
//函数
public String getHTMLcontent(InputStream is,long contentLength,String charSet) throws IOException
{
byte[] responseBody = null;
if (is != null) {
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();
}
return EncodingUtil.getString(responseBody, charSet);
}
相关文章推荐
- 关于PHP CURL post数据时数据过大导致无法接收返回信息的处理方法
- spring 统一处理http request请求返回数据,返回加密信息等
- Java 通过httpClient Post方式提交xml,并从服务端返回数据
- httpClient请求及返回数据的处理
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- httpClient post 数据传输和处理
- httpclient的post请求并处理json格式响应数据
- 一个完整的通过encoding/json信息传递格式,采用http.POST利用进行与服务器通信,并对返回结果进行解析处理的典型案例
- Java 通过HttpClient Post方式提交json,并从服务端返回json数据
- HttpClient get和HttpClient Post请求的方式获取服务器的返回数据
- spring-boot统一处理返回给前台的数据格式(避免返回异常堆栈信息,干扰用户体验)
- Android 如何用HttpClient 以Post方式提交数据并添加http头信息
- 网络数据的处理之HttpClient
- HttpClient用Post请求网络数据
- (三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- httpclient post 发送Json数据
- java处理http发送的数据转化为json格式并返回遇到的问题
- .net调用别人的http页面,返回结果,url中有中文的处理,get和post两种方法
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)