I'm trying to use OkHttp library to send post request to API with some url parameters. Following this blog post I have this code so far:
public String okHttpRequest() throws IOException{
OkHttpClient client = new OkHttpClient();
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
HttpUrl.Builder urlBuilder = HttpUrl.parse("myurl").newBuilder();
urlBuilder.addQueryParameter("username","username");
urlBuilder.addQueryParameter("password","7777");
String url = urlBuilder.build().toString();
Request request = new Request.Builder()
.url(url)
.build();
//HERE EXCEPTION IS THROWN
Response response = client.newCall(request).execute();
return response.body().string();
}
The exception is:
javax.net.ssl.SSLPeerUnverifiedException: Hostname {domain} not verified:
UPDATE
Code for com.squareup.okhttp3:okhttp:3.0.1
mTextView = (TextView) findViewById(R.id.textView);
mHandler = new Handler(Looper.getMainLooper());
final Request request = new Request.Builder()
.url("https://...")
.post(formBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Log.e(LOG_TAG, e.toString());
mHandler.post(new Runnable() {
@Override
public void run() {
String message = request.toString() + "\r\n" + e.toString();
mTextView.setText(message);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
final String message = jsonObject.toString(5);
Log.i(LOG_TAG, message);
mHandler.post(new Runnable() {
@Override
public void run() {
mTextView.setText(message);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Because your project uses OkHttp v3.0.0-RC1, so to fix that Exception, your code should be as the following sample:
OkHttpClient client = new OkHttpClient.Builder()
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
})
.build();
Request request = new Request.Builder()
.url("https://...")
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(final Request request, final IOException e) {
// do something...
}
@Override
public void onResponse(Response response) throws IOException {
// do something...
}
});
However, instead of return true;
above, I suggest that you read Google's documentation about Common Problems with Hostname Verification for more information.
One more useful link is OkHttp's HTTPS wiki.
Hope it helps!
P/S: please note that I use async way of OkHttp (at client.newCall(request).enqueue(new Callback()...), you can also use sync way as your code.
Apparently you try to connect to a SSL website (https), therefore you need to add a SSLSocketFactory
, below little code snippet.
OkHttpClient client = new OkHttpClient();
client.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
For more details see this page or this, it should help you.
If you want to "trust all certificates", see this example but it`s not recommanded and should only be used for testing purposes!
Check SSLSession hostname and your connection host name...
OkHttpClient client = new OkHttpClient();
client.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
System.out.println("Warning: URL host '" + urlHostName
+ "' is different to SSLSession host '"
+ session.getPeerHost() + "'.");
}
return true;
}
});