保持更新ing
public class HttpClientUtil {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
private static final int HTTP_TIMEOUT=180000;
public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");
public static String postRemote(String remoteUrl, Map<String, String> dataMap) throws Exception {
OkHttpClient client = new OkHttpClient();
FormBody.Builder builder = new FormBody.Builder();
if (dataMap != null && !dataMap.isEmpty()) {
for (Map.Entry<String, String> entry : dataMap.entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
}
FormBody formBody = builder.build();
Request request = new Request.Builder().url(remoteUrl).post(formBody).build();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
} else {
logger.error("调用远程的系统异常(postRemote): " + response);
throw new Exception("调用远程的系统异常(remoteUrl =" + remoteUrl + ") :" + response);
}
}
}
在这里不提供封装httpclient 有很大局限,不用。ok经过生产压力 非常适合生产。