68 lines
2.2 KiB
Java
68 lines
2.2 KiB
Java
/*
|
|||
|
|
* This file is part of the Wildfire Chat package.
|
||
|
|
* (c) Heavyrain2012 <[email protected]>
|
||
|
|
*
|
||
|
|
* For the full copyright and license information, please view the LICENSE
|
||
|
|
* file that was distributed with this source code.
|
||
|
|
*/
|
||
|
|
|
||
|
|
package win.liyufan.im;
|
||
|
|
|
||
|
|
import io.netty.util.internal.StringUtil;
|
||
import okhttp3.*;
|
|||
import org.apache.http.HttpResponse;
|
|||
|
|
import org.apache.http.HttpStatus;
|
||
|
|
import org.apache.http.client.HttpClient;
|
||
|
|
import org.apache.http.client.methods.HttpPost;
|
||
|
|
import org.apache.http.entity.StringEntity;
|
||
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||
|
|
import org.apache.http.params.CoreConnectionPNames;
|
||
|
|
import org.slf4j.Logger;
|
||
|
|
import org.slf4j.LoggerFactory;
|
||
|
|
|
||
|
|
import java.io.BufferedReader;
|
||
import java.io.IOException;
|
|||
import java.io.InputStreamReader;
|
|||
|
|
import java.nio.charset.Charset;
|
||
|
|
|
||
|
|
|
||
|
|
public class HttpUtils {
|
||
|
|
private static final Logger LOG = LoggerFactory.getLogger(HttpUtils.class);
|
||
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
|||
|
|
static private OkHttpClient client = new OkHttpClient();
|
||
|
|||
public static void httpJsonPost(final String url, final String jsonStr){
|
|||
LOG.info("POST to {} with data {}", url, jsonStr);
|
|||
|
|
if (StringUtil.isNullOrEmpty(url)) {
|
||
|
|
LOG.error("http post failure with empty url");
|
||
return;
|
|||
}
|
|||
|
|
|
||
RequestBody body = RequestBody.create(JSON, jsonStr);
|
|||
|
|
Request request = new Request.Builder()
|
||
|
|
.url(url)
|
||
|
|
.post(body)
|
||
|
|
.build();
|
||
|
|
client.newCall(request).enqueue(new Callback() {
|
||
|
|
@Override
|
||
|
|
public void onFailure(Call call, IOException e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
LOG.info("POST to {} with data {} failure", url, jsonStr);
|
||
}
|
|||
|
|||
|
|
@Override
|
||
|
|
public void onResponse(Call call, Response response) throws IOException {
|
||
LOG.info("POST to {} success with response: {}", url, response.body());
|
|||
try {
|
|||
|
|
if (response.body() != null) {
|
||
|
|
response.body().close();
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
e.printStackTrace();
|
||
|
|
}
|
||
}
|
|||
});
|
|||
}
|
|||
|
|
|
||
|
|
}
|