?
private static String testOkhttp(String url, HashMap<String, String> headers, HashMap<String, String> params){
OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = client.newBuilder();
builder.connectTimeout(10, TimeUnit.SECONDS);
builder.readTimeout(30,TimeUnit.SECONDS);
builder.writeTimeout(30,TimeUnit.SECONDS);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
builder.build();
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.addHeader("Content-Type", "application/json; charset=utf-8");
if (headers!=null){
for (String s : headers.keySet()) {
requestBuilder.addHeader(s,headers.get(s));
}
}
if (params!=null){
FormBody.Builder forbody=new FormBody.Builder();
for (String s : params.keySet()) {
forbody.add(s,params.get(s));
}
requestBuilder.post(forbody.build());
}
requestBuilder.url(url);
Request request = requestBuilder.build();
try {
Response response = client.newCall(request).execute();
if (response != null && response.code() == 200) {
return response.body().string();
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
|