我試圖使用OkHttp庫發送後請求API與一些URL參數。繼this blog post我有這樣的代碼至今:SSLPeerUnverifiedException OkHttp?
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();
}
唯一的例外是:
javax.net.ssl.SSLPeerUnverifiedException: Hostname {domain} not verified:
你真是太好了@BNK!請求現在被髮送到測試網址以獲取發佈請求。可以添加參數來請求url?/ login?username = alvin&password = 2186等 – Alvin
IMO,你應該閱讀https://github.com/square/okhttp/wiki/Recipes – BNK
@Alvin參數,你可以閱讀更多在http://stackoverflow.com/questions/30142626/how-to-add- query-parameters-to-a-http-get-request-by-okhttp – BNK