Commit 687cbae3 authored by Quxl's avatar Quxl

x

parent c20aa7c6
package com.egolm.sso.util; package com.egolm.sso.util;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
...@@ -10,6 +11,8 @@ import java.util.Map; ...@@ -10,6 +11,8 @@ import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
public class HttpUtil { public class HttpUtil {
public static String get(String requestUrl, Map<String, String> headers) { public static String get(String requestUrl, Map<String, String> headers) {
...@@ -77,6 +80,36 @@ public class HttpUtil { ...@@ -77,6 +80,36 @@ public class HttpUtil {
try { try {
String requestBody = toQueryString(parameters); String requestBody = toQueryString(parameters);
requestUrl += (requestUrl.contains("?") ? "&" : "?") + requestBody; requestUrl += (requestUrl.contains("?") ? "&" : "?") + requestBody;
System.out.println(requestUrl);
System.out.println(JSON.toJSONString(parameters));
System.out.println(JSON.toJSONString(headers));
URL getUrl = new URL(requestUrl);
connection = (HttpURLConnection) getUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/json");
if (headers != null) {
for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key));
}
}
connection.connect();
return responseBody(connection);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
connection.disconnect();
}
}
public static String postRaw(String requestUrl, Map<String, Object> parameters, Map<String, String> headers) {
HttpURLConnection connection = null;
OutputStream os = null;
try {
System.out.println(requestUrl);
System.out.println(JSON.toJSONString(parameters));
System.out.println(JSON.toJSONString(headers));
URL getUrl = new URL(requestUrl); URL getUrl = new URL(requestUrl);
connection = (HttpURLConnection) getUrl.openConnection(); connection = (HttpURLConnection) getUrl.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
...@@ -88,10 +121,19 @@ public class HttpUtil { ...@@ -88,10 +121,19 @@ public class HttpUtil {
} }
} }
connection.connect(); connection.connect();
os = connection.getOutputStream();
String rawData = JSON.toJSONString(parameters);
os.write(rawData.getBytes());
os.flush();
return responseBody(connection); return responseBody(connection);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} finally { } finally {
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
connection.disconnect(); connection.disconnect();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment