Java Code Examples for cn.hutool.http.HttpResponse#bodyBytes()

The following examples show how to use cn.hutool.http.HttpResponse#bodyBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public byte[] getBytes(String url, String ua, String refer) {
	logger.info("getBytes=" + url);

	try {
		HttpResponse response = addHeader(HttpRequest.get(url).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT, _accept).header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info("response header:" + getRespHeaderStr(response));
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return getBytes(location, ua, refer);
		} else if (statusCode == 200) {
			return response.bodyBytes();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example 2
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public byte[] postBytes(String url, Map<String, Object> list, String ua, String refer) {
	logger.info("postBytes=" + url);

	try {
		HttpResponse response = addHeader(HttpRequest.post(url).form(list).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT, _accept).header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info("response header:" + getRespHeaderStr(response));
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return postBytes(location, list, ua, refer);
		} else if (statusCode == 200) {
			return response.bodyBytes();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example 3
Source File: ZIMuKuCommon.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
/**
 * 下载字幕
 * @param url
 * @return
 */
public static JSONObject downContent(String url) {
	String result = httpGet(baseUrl+url);
	String downUrl = RegexUtil.getMatchStr(result, 
			"<a\\s+id=\"down1\"\\s+href=\"([^\"]*/dld/[\\w]+\\.html)\""
			, Pattern.DOTALL);
	if(downUrl == null)return null;
	if(!downUrl.startsWith("http")) {
		downUrl = baseUrl + downUrl;
	}
	
	result = httpGet(downUrl);
	if(result == null)return null;
	//System.out.println(result);
	JSONArray resList = RegexUtil.getMatchList(result, 
			"<li><a\\s+rel=\"nofollow\"\\s+href=\"([^\"]*/download/[^\"]+)\"", Pattern.DOTALL);
	if(resList == null || resList.size() == 0 || resList.getJSONArray(0).size() == 0)return null;
	//HtHttpUtil.http.debug=true;
	HttpResponse httpResp = HtHttpUtil.http.getResponse(addBaseUrl(resList.getJSONArray(0).getStr(0)), null, downUrl);
	int i = 0;
	while(httpResp == null && resList.size() > ++i) {
		httpResp = HtHttpUtil.http.getResponse(addBaseUrl(resList.getJSONArray(1).getStr(0)), null, downUrl);
	}
	//System.out.println(httpResp);
	if(httpResp == null)return null;
	String filename = HtHttpUtil.getFileName(httpResp);
	byte[] data = httpResp.bodyBytes();
	//System.out.println(filename);
	JSONObject resp = new JSONObject();
	resp.put("filename", filename);
	resp.put("ext", StringUtil.extName(filename).toLowerCase());
	resp.put("data", Base64.encode(data));
	
	return resp;
}
 
Example 4
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public byte[] getBytes(String url, String ua, String refer) {
	logger.info("getBytes=" + url);

	try {
		HttpResponse response = addHeader(HttpRequest.get(url).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT, _accept).header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info("response header:" + getRespHeaderStr(response));
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return getBytes(location, ua, refer);
		} else if (statusCode == 200) {
			return response.bodyBytes();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example 5
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public byte[] postBytes(String url, Map<String, Object> list, String ua, String refer) {
	logger.info("postBytes=" + url);

	try {
		HttpResponse response = addHeader(HttpRequest.post(url).form(list).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT, _accept).header(Header.ACCEPT_ENCODING, _accept_encoding)
				.header(Header.REFERER, refer != null ? refer : url)
				.header(Header.ACCEPT_LANGUAGE, _accept_language).timeout(_time_out)).execute();

		if (debug) {
			logger.info("response header:" + getRespHeaderStr(response));
		}
		int statusCode = response.getStatus();
		if (statusCode == 301 || statusCode == 302) {
			String location = response.header("Location");
			if (!location.toLowerCase().startsWith("http")) {
				location = StringUtil.getBaseUrl(url) + location;
			}
			return postBytes(location, list, ua, refer);
		} else if (statusCode == 200) {
			return response.bodyBytes();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example 6
Source File: ZIMuKuCommon.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
/**
 * 下载字幕
 * @param url
 * @return
 */
public static JSONObject downContent(String url) {
	String result = httpGet(baseUrl+url);
	String downUrl = RegexUtil.getMatchStr(result, 
			"<a\\s+id=\"down1\"\\s+href=\"([^\"]*/dld/[\\w]+\\.html)\""
			, Pattern.DOTALL);
	if(downUrl == null)return null;
	if(!downUrl.startsWith("http")) {
		downUrl = baseUrl + downUrl;
	}
	
	result = httpGet(downUrl);
	if(result == null)return null;
	//System.out.println(result);
	JSONArray resList = RegexUtil.getMatchList(result, 
			"<li><a\\s+rel=\"nofollow\"\\s+href=\"([^\"]*/download/[^\"]+)\"", Pattern.DOTALL);
	if(resList == null || resList.size() == 0 || resList.getJSONArray(0).size() == 0)return null;
	//HtHttpUtil.http.debug=true;
	HttpResponse httpResp = HtHttpUtil.http.getResponse(addBaseUrl(resList.getJSONArray(0).getStr(0)), null, downUrl);
	int i = 0;
	while(httpResp == null && resList.size() > ++i) {
		httpResp = HtHttpUtil.http.getResponse(addBaseUrl(resList.getJSONArray(1).getStr(0)), null, downUrl);
	}
	//System.out.println(httpResp);
	if(httpResp == null)return null;
	String filename = HtHttpUtil.getFileName(httpResp);
	byte[] data = httpResp.bodyBytes();
	//System.out.println(filename);
	JSONObject resp = new JSONObject();
	resp.put("filename", filename);
	resp.put("ext", StringUtil.extName(filename).toLowerCase());
	resp.put("data", Base64.encode(data));
	
	return resp;
}