cn.hutool.http.Header Java Examples

The following examples show how to use cn.hutool.http.Header. 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: WebUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static String getLocation(String url, String cookie) {
    try {
        HttpResponse response = get(url, 0, new Hashtable<String, String>() {{
            put("Cookie", cookie);
        }}, false);
        if (response == null) {
            return url;
        }
        String location = response.header(Header.LOCATION);
        response.close();
        return location;
    } catch (Exception ex) {
        return "";
    }
}
 
Example #2
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String get(String url, String charset, String ua, String refer) {
	logger.info("get=" + url);

	try {
		HttpResponse response = addHeader(HttpRequest.get(url).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT_CHARSET, charset).charset(charset)
				.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.toString());
		}
		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 get(location, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example #3
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String post(String url, Map<String, Object> list, String charset, String ua, String refer) {
	logger.info("post=" + url);
	try {
		HttpResponse response = addHeader(HttpRequest.post(url).form(list).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT_CHARSET, charset).charset(charset)
				.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 post(location, list, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example #4
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String postStream(String url, String data, String charset, String ua, String refer) {
	logger.info("postStream=" + url);
	try {
		HttpResponse response = addHeader(HttpRequest.post(url).body(data, _stream_media_type)
				.header(Header.USER_AGENT, ua).header(Header.ACCEPT_CHARSET, charset)
				.charset(charset).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 postStream(location, data, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} 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 HttpResponse getResponse(String url, String ua, String refer) {
	logger.info("getResponse=" + 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 getResponse(location, ua, refer);
		} else if (statusCode == 200) {
			return response;
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example #6
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 #7
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 #8
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String get(String url, String charset, String ua, String refer) {
	logger.info("get=" + url);

	try {
		HttpResponse response = addHeader(HttpRequest.get(url).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT_CHARSET, charset).charset(charset)
				.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.toString());
		}
		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 get(location, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example #9
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String post(String url, Map<String, Object> list, String charset, String ua, String refer) {
	logger.info("post=" + url);
	try {
		HttpResponse response = addHeader(HttpRequest.post(url).form(list).header(Header.USER_AGENT, ua)
				.header(Header.ACCEPT_CHARSET, charset).charset(charset)
				.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 post(location, list, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example #10
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public String postStream(String url, String data, String charset, String ua, String refer) {
	logger.info("postStream=" + url);
	try {
		HttpResponse response = addHeader(HttpRequest.post(url).body(data, _stream_media_type)
				.header(Header.USER_AGENT, ua).header(Header.ACCEPT_CHARSET, charset)
				.charset(charset).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 postStream(location, data, charset, ua, refer);
		} else if (statusCode == 200) {
			return response.body();
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}

}
 
Example #11
Source File: HtHttpUtil.java    From SubTitleSearcher with Apache License 2.0 5 votes vote down vote up
public HttpResponse getResponse(String url, String ua, String refer) {
	logger.info("getResponse=" + 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 getResponse(location, ua, refer);
		} else if (statusCode == 200) {
			return response;
		} else {
			logger.error(url + ", failed: " + statusCode);
			return null;
		}
	} catch (Exception e) {
		logger.error(e);
		return null;
	} finally {
	}
}
 
Example #12
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 #13
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 {
	}
}