Skip to content

Commit

Permalink
增加postJSON方法
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix committed Jul 12, 2015
1 parent b834a36 commit 8c07f18
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/java/edu/scup/io/HttpClient.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.scup.io;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -15,6 +16,7 @@
public class HttpClient {
private static final Logger logger = LoggerFactory.getLogger(HttpClient.class);
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.13 Safari/537.36";
private static final ObjectMapper objectMapper = new ObjectMapper();
public static Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 1080));
public static boolean useProxy;
private static final int socketReadTimeout = 30000;
Expand Down Expand Up @@ -84,6 +86,40 @@ public static String post(String url, byte[] body, String contentType) throws IO
}
}

public static String postJSON(String url, Object request, Map<String, String> headers) throws IOException {
try {
HttpURLConnection conn = openConnection(url);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json");
if (headers != null) {
for (String key : headers.keySet()) {
conn.setRequestProperty(key, headers.get("key"));
}
}
conn.getOutputStream().write(objectMapper.writeValueAsBytes(request));

if (conn.getResponseCode() != 200) {
logger.error("response error {},request {}", IOUtils.toString(conn.getErrorStream()), objectMapper.writeValueAsString(request));
return "";
}

String contentType = conn.getContentType();
String encoding = "utf-8";
if (contentType != null && contentType.indexOf("charset=") > 0) {
encoding = contentType.split("charset=")[1];
}

InputStream is = conn.getInputStream();
byte[] resp = IOUtils.toByteArray(is);
conn.disconnect();
return new String(resp, encoding);
} catch (MalformedURLException | URISyntaxException e) {
logger.error("", e);
return "";
}
}

public static String getRedirectUrl(String url) throws URISyntaxException, IOException {
HttpURLConnection conn = openConnection(url);
conn.setInstanceFollowRedirects(false);
Expand Down

0 comments on commit 8c07f18

Please sign in to comment.