- Retrofit注解
- Interceptor拦截器的使用
- 配置https证书
Retrofit注解
【Android】Retrofit网络请求参数注解GET、POST、DELETE还有PUT的请求,说明@Path、@Query、@QueryMap、@Body、@Field的用法。
http://www.jianshu.com/p/7687365aa946
Interceptor拦截器的使用
package okhttp3;
import java.io.IOException;
import javax.annotation.Nullable;
/**
* Observes, modifies, and potentially short-circuits requests going out and the corresponding
* responses coming back in. Typically interceptors add, remove, or transform headers on the request
* or response.
* 观察,修改 潜在的拦截(短路)请求,并返回相应的响应(response)。拦截器可以在请求或响应(rquest or response)中添加,删除,修改headers
*
*/
public interface Interceptor {
Response intercept(Chain chain) throws IOException;
interface Chain {
//拦截发出去的请求
Request request();
//继续一个请求
Response proceed(Request request) throws IOException;
/**
* Returns the connection the request will be executed on. This is only available in the chains
* of network interceptors; for application interceptors this is always null.
* 返回请求将要直行的connection
* 这只适用于网络拦截器的chains。 对于应用程序拦截器,这总是空的。
*/
@Nullable Connection connection();
}
}
配置https 证书
主要使用的是OkHttpClient.Builder.sslSocketFactory(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager)
方法
.hostnameVerifier() 用来验证主机名
private static OkHttpClient getOKhttpClient() {
TrustManagerFactory trustManagerFactory = null;
InputStream is = null;
try {
// 取到证书的输入流
is = MyApplication.getInstance().getAssets().open("your.crt");
CertificateFactory cf = CertificateFactory.getInstance("X.509");
final Certificate ca;
try {
ca = cf.generateCertificate(is);
}finally {
is.close();
}
TrustManager[] trustManagers = new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//校验服务器证书
for (X509Certificate cert : chain) {
cert.checkValidity();
try {
cert.verify(ca.getPublicKey());
// isServerTrusted = true;
} catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException e) {
e.printStackTrace();
// isServerTrusted = false;
}
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}};
HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
if("yourXX.com".equals(hostname)){
return true;
}
return false;
}
};
X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
// 用 TrustManager 初始化一个 SSLContext
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null,trustManagers, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
// .sslSocketFactory(aDefault.getSocketFactory())
.hostnameVerifier(DO_NOT_VERIFY)
.sslSocketFactory(sslSocketFactory, trustManager)
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
.addHeader("Accept-Encoding", "gzip, deflate")
.addHeader("Connection", "keep-alive")
.addHeader("Accept", "*/*")
.addHeader("token","your token ")
.build();
return chain.proceed(request);
}
})
.build();
return okHttpClient;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}