Support
Quality
Security
License
Reuse
kandi has reviewed okhttputils and discovered the below as its top functions. This is intended to give you an instant insight into okhttputils implemented functionality, and help decide if they suit your requirements.
[停止维护]okhttp的辅助类
用法
compile 'com.zhy:okhttputils:2.6.2'
配置OkhttpClient
public class MyApplication extends Application
{
@Override
public void onCreate()
{
super.onCreate();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
// .addInterceptor(new LoggerInterceptor("TAG"))
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
}
}
对于Cookie(包含Session)
CookieJarImpl cookieJar = new CookieJarImpl(new PersistentCookieStore(getApplicationContext()));
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.cookieJar(cookieJar)
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
对于Log
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggerInterceptor("TAG"))
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
对于Https
HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
//其他配置
.build();
OkHttpUtils.initClient(okHttpClient);
GET请求
String url = "http://www.csdn.net/";
OkHttpUtils
.get()
.url(url)
.addParams("username", "hyman")
.addParams("password", "123")
.build()
.execute(new StringCallback()
{
@Override
public void onError(Request request, Exception e)
{
}
@Override
public void onResponse(String response)
{
}
});
POST请求
OkHttpUtils
.post()
.url(url)
.addParams("username", "hyman")
.addParams("password", "123")
.build()
.execute(callback);
Post JSON
OkHttpUtils
.postString()
.url(url)
.content(new Gson().toJson(new User("zhy", "123")))
.mediaType(MediaType.parse("application/json; charset=utf-8"))
.build()
.execute(new MyStringCallback());
Post File
OkHttpUtils
.postFile()
.url(url)
.file(file)
.build()
.execute(new MyStringCallback());
Post表单形式上传文件
OkHttpUtils.post()//
.addFile("mFile", "messenger_01.png", file)//
.addFile("mFile", "test1.txt", file2)//
.url(url)
.params(params)//
.headers(headers)//
.build()//
.execute(new MyStringCallback());
自定义CallBack
public abstract class UserCallback extends Callback<User>
{
@Override
public User parseNetworkResponse(Response response) throws IOException
{
String string = response.body().string();
User user = new Gson().fromJson(string, User.class);
return user;
}
}
OkHttpUtils
.get()//
.url(url)//
.addParams("username", "hyman")//
.addParams("password", "123")//
.build()//
.execute(new UserCallback()
{
@Override
public void onError(Request request, Exception e)
{
mTv.setText("onError:" + e.getMessage());
}
@Override
public void onResponse(User response)
{
mTv.setText("onResponse:" + response.username);
}
});
下载文件
OkHttpUtils//
.get()//
.url(url)//
.build()//
.execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(), "gson-2.2.1.jar")//
{
@Override
public void inProgress(float progress)
{
mProgressBar.setProgress((int) (100 * progress));
}
@Override
public void onError(Request request, Exception e)
{
Log.e(TAG, "onError :" + e.getMessage());
}
@Override
public void onResponse(File file)
{
Log.e(TAG, "onResponse :" + file.getAbsolutePath());
}
});
显示图片
OkHttpUtils
.get()//
.url(url)//
.build()//
.execute(new BitmapCallback()
{
@Override
public void onError(Request request, Exception e)
{
mTv.setText("onError:" + e.getMessage());
}
@Override
public void onResponse(Bitmap bitmap)
{
mImageView.setImageBitmap(bitmap);
}
});
上传下载的进度显示
new Callback<T>()
{
//...
@Override
public void inProgress(float progress)
{
//use progress: 0 ~ 1
}
}
HEAD、DELETE、PUT、PATCH
OkHttpUtils
.put()//also can use delete() ,head() , patch()
.requestBody(RequestBody.create(null, "may be something"))//
.build()//
.execute(new MyStringCallback());
同步的请求
Response response = OkHttpUtils
.get()//
.url(url)//
.tag(this)//
.build()//
.execute();
取消单个请求
RequestCall call = OkHttpUtils.get().url(url).build();
call.cancel();
根据tag取消请求
OkHttpUtils
.get()//
.url(url)//
.tag(this)//
.build()//
@Override
protected void onDestroy()
{
super.onDestroy();
//可以取消同一个tag的
OkHttpUtils.cancelTag(this);//取消以Activity.this作为tag的请求
}
混淆
#okhttputils
-dontwarn com.zhy.http.**
-keep class com.zhy.http.**{*;}
#okhttp
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}
#okio
-dontwarn okio.**
-keep class okio.**{*;}
QUESTION
android java how to protect/ secure my Hardcoded Password
Asked 2021-Jul-31 at 19:04i want to make CertPinning, sending request with clientcertificate. for that i need to put my password hardcoded to use it in the keystore init:
keyManagerFactory.init(keyStore, Password.toCharArray());
how can I protect it?
File pKeyFile;
InputStream caInput = new BufferedInputStream(OkHttpUtils.class.getClassLoader().getResourceAsStream("assets/acptamancli.pfx"));
String Password= "MyPassword!";
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("X509");
KeyStore keyStore = KeyStore.getInstance("PKCS12");
// InputStream keyInput = new FileInputStream(caInput);
keyStore.load(caInput, Password.toCharArray());
caInput.close();
keyManagerFactory.init(keyStore, Password.toCharArray());
SSLContext context = SSLContext.getInstance("TLS");
context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
SSLSocketFactory sockFact = context.getSocketFactory();
con.setSSLSocketFactory(sockFact);
/* Send the request */
OutputStream outputStream = con.getOutputStream();
outputStream.write(requestParams.getBytes("UTF-8"));
outputStream.close();
InputStream inputStream;
if(comparePKeys) {
// Check for errors
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inputStream = con.getInputStream();
} else {
inputStream = con.getErrorStream();
}
} else{
inputStream = con.getErrorStream();
}
// Process the response
InputStream is = inputStream;
int i;
BufferedReader reader;
String line = null;
reader = new BufferedReader(new InputStreamReader(inputStream));
String result = reader.readLine();
inputStream.close();
callback.invoke(null, result);
} catch (Exception e) {
e.printStackTrace();
}
ANSWER
Answered 2021-Jul-31 at 19:02You don't. Anything present in the distributed package can be accessed by someone who has it.
Note that you're mixing two separate concepts, certificate pinning and client certificates. Client certificates are very rarely used outside an internal corporate environment, and pinned public certificates don't need to be hidden.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Save this library and start creating your kit