Http.get(String, Http.Conf), post(String, Http.Conf) =>

Http.get(Http.Conf), Http.post(Http.Conf)
master
mjkhan21 2 years ago
parent e19abaee6d
commit 5c47d8c195

@ -17,8 +17,8 @@ import cokr.xit.foundation.AbstractComponent;
/**Http /**Http
* <p> . * <p> .
* <pre><code> new Http().get( * <pre><code> new Http().get(
* "https://...",
* new Http.Conf() * new Http.Conf()
* .url("https://...")
* // .header("Content-Type", "application/x-www-form-urlencoded") * // .header("Content-Type", "application/x-www-form-urlencoded")
* .param("param0", "value0") * .param("param0", "value0")
* .param("param1", "value1") * .param("param1", "value1")
@ -27,6 +27,8 @@ import cokr.xit.foundation.AbstractComponent;
* @author mjkhan * @author mjkhan
*/ */
public class Http extends AbstractComponent { public class Http extends AbstractComponent {
public static final String FORM = "application/x-www-form-urlencoded";
public static final String JSON = "application/json";
/**http /**http
* @author mjkhan * @author mjkhan
*/ */
@ -43,12 +45,12 @@ public class Http extends AbstractComponent {
/** . /** .
* @return * @return
*/ */
protected String charset() { private String charset() {
return ifEmpty(charset, () -> "UTF-8"); return ifEmpty(charset, () -> "UTF-8");
} }
/** . /** .
* @param charset * @param charset . UTF-8.
* @return Conf * @return Conf
*/ */
public Conf charset(String charset) { public Conf charset(String charset) {
@ -60,21 +62,30 @@ public class Http extends AbstractComponent {
* @return URL * @return URL
* @throws Exception * @throws Exception
*/ */
protected URL url() throws Exception { private URL url() throws Exception {
return new URL(url); return new URL(notEmpty(url, "url"));
} }
/**http . "GET" /**http url .
* @return http * @param url url
* @return Conf
*/ */
protected String method() { public Conf url(String url) {
this.url = url;
return this;
}
/**http .
* @return http . "GET"
*/
private String method() {
return ifEmpty(method, "GET"); return ifEmpty(method, "GET");
} }
/** . /** .
* @return * @return
*/ */
protected String params() { private String params() {
return !isEmpty(params) ? params.entrySet().stream().map(this::encode).collect(Collectors.joining("&")) : ""; return !isEmpty(params) ? params.entrySet().stream().map(this::encode).collect(Collectors.joining("&")) : "";
} }
@ -82,7 +93,7 @@ public class Http extends AbstractComponent {
* @param entry * @param entry
* @return * @return
*/ */
protected String encode(Map.Entry<String, String> entry) { private String encode(Map.Entry<String, String> entry) {
try { try {
return URLEncoder.encode(entry.getKey(), charset()) + "=" + URLEncoder.encode(entry.getValue(), charset()); return URLEncoder.encode(entry.getKey(), charset()) + "=" + URLEncoder.encode(entry.getValue(), charset());
} catch (Exception e) { } catch (Exception e) {
@ -117,7 +128,7 @@ public class Http extends AbstractComponent {
/** http . /** http .
* @param http HttpURLConnection * @param http HttpURLConnection
*/ */
protected void setHeaders(HttpURLConnection http) { private void setHeaders(HttpURLConnection http) {
if (isEmpty(headers)) return; if (isEmpty(headers)) return;
headers.forEach(http::setRequestProperty); headers.forEach(http::setRequestProperty);
@ -126,7 +137,7 @@ public class Http extends AbstractComponent {
/**http . 10000 /**http . 10000
* @return http * @return http
*/ */
protected int timeout() { private int timeout() {
return Math.max(timeout, 10000); return Math.max(timeout, 10000);
} }
@ -187,35 +198,32 @@ public class Http extends AbstractComponent {
} }
/** url GET . /** url GET .
* @param url url
* @param conf * @param conf
*/ */
public void get(String url, Conf conf) { public void get(Conf conf) {
if (conf == null) if (conf == null)
conf = new Conf(); conf = new Conf();
conf.method = "GET"; conf.method = "GET";
request(url, conf); request(conf);
} }
/** url POST . /** url POST .
* @param url url
* @param conf * @param conf
*/ */
public void post(String url, Conf conf) { public void post(Conf conf) {
if (conf == null) if (conf == null)
conf = new Conf(); conf = new Conf();
conf.method = "POST"; conf.method = "POST";
request(url, conf); request(conf);
} }
/** url , conf . /** url , conf .
* @param url url * @param url url
* @param conf * @param conf
*/ */
protected void request(String url, Conf conf) { private void request(Conf conf) {
HttpURLConnection http = null; HttpURLConnection http = null;
try { try {
conf.url = url;
http = (HttpURLConnection)conf.url().openConnection(); http = (HttpURLConnection)conf.url().openConnection();
http.setRequestMethod(conf.method()); http.setRequestMethod(conf.method());
http.setUseCaches(false); http.setUseCaches(false);
@ -243,15 +251,15 @@ public class Http extends AbstractComponent {
* @param http HttpURLConnection * @param http HttpURLConnection
* @throws Exception * @throws Exception
*/ */
protected void setParams(Conf conf, HttpURLConnection http) throws Exception { private void setParams(Conf conf, HttpURLConnection http) throws Exception {
String params = conf.params(); String params = conf.params();
if (!isEmpty(params)) { if (isEmpty(params)) return;
http.setDoOutput(true);
DataOutputStream out = new DataOutputStream(http.getOutputStream()); http.setDoOutput(true);
out.writeBytes(params); DataOutputStream out = new DataOutputStream(http.getOutputStream());
out.flush(); out.writeBytes(params);
out.close(); out.flush();
} out.close();
} }
/**http . /**http .
@ -259,7 +267,7 @@ public class Http extends AbstractComponent {
* @return http * @return http
* @throws Exception * @throws Exception
*/ */
protected Response getResponse(HttpURLConnection http) throws Exception { private Response getResponse(HttpURLConnection http) throws Exception {
Response resp = new Response(); Response resp = new Response();
resp.status = http.getResponseCode(); resp.status = http.getResponseCode();
@ -284,7 +292,7 @@ public class Http extends AbstractComponent {
* @param t Throwable * @param t Throwable
* @return http * @return http
*/ */
protected Response getResponse(Throwable t) { private Response getResponse(Throwable t) {
Response resp = new Response(); Response resp = new Response();
resp.status = 500; resp.status = 500;
resp.response = rootCause(t).getMessage(); resp.response = rootCause(t).getMessage();

Loading…
Cancel
Save