最近在网上看看到很多抓包的教程,这里先来分享一下关于抓包的教程,
抓包教程
用Fiddler对Android应用进行抓包,关于这个教程我提醒一下[第二部]设置完成之后,一定要重启Fiddler才可以
问题来了,作为一个android程序员,你的应用可能被抓包吗?用上面的教程试一试吧..............?
从上面的抓包教程中,我们可以看到,抓包其实上是先设置一个本地的代理,然后在代理中记录本网络中网络请求.
平时开发的时候自己封装的网络框架一不小心,可能存在抓包的危险,如何避免呢........ 其实很简单啦.百度一下就可以了, 嘻嘻..........
我用的是okhttp的请求,在请求添加如下代码
大神忽略以上以及以下内容 .................
给大家分享一下我的网络请求封装------工具
/** * @author 新空 * @time 2018/3/25/025 23:47 * @week 星期日 */class RetrofitManager : Interceptor { var mOkHttpClient: OkHttpClient? = null var url: String = "http://xxxxxx/api/" var service: Api var retrofit: Retrofit? = null //长缓存有效期为7天 val CACHE_STALE_LONG = "60 * 60 * 24 * 7" //查询缓存的Cache-Control设置,为if-only-cache时只查询缓存而不会请求服务器,max-stale可以配合设置缓存失效时间 val CACHE_CONTROL_CACHE = "only-if-cached, max-stale=" + CACHE_STALE_LONG //查询网络的Cache-Control设置,头部Cache-Control设为max-age=0时则不会使用缓存而请求服务器 val CACHE_CONTROL_NETWORK = "max-age=0" init { intOkHttp() service = Retrofit.Builder() .baseUrl(url) .client(mOkHttpClient) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(CustomGsonConverterFactory.create()) .build() .create(Api::class.java) } private fun intOkHttp() { val intercept = HttpLoggingInterceptor() intercept.level = HttpLoggingInterceptor.Level.BODY if (mOkHttpClient == null) { // val cach = Cache(File(MyApplication.getInstance().cacheDir, "File_Kotlin"), 14 * 1024 * 100) mOkHttpClient = OkHttpClient.Builder() .retryOnConnectionFailure(true) .addInterceptor(object : Interceptor { override fun intercept(chain: Interceptor.Chain?): Response { val header = chain!!.request().newBuilder() .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") .addHeader("Connection", "keep-alive") .addHeader("Accept", "*/*") .addHeader("Cookie", "add cookies here") return chain!!.proceed(header.build()) } }) .proxy(Proxy.NO_PROXY) .addInterceptor(intercept) .connectTimeout(15, TimeUnit.SECONDS) .build() } } companion object { fun builder(): RetrofitManager { return RetrofitManager() } } override fun intercept(chain: Interceptor.Chain?): Response { val header = chain!!.request().newBuilder() .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") .addHeader("Connection", "keep-alive") .addHeader("Accept", "*/*") .addHeader("Cookie", "add cookies here") return chain!!.proceed(header.build()) } //用T fun <T> toSubscribe(observable: Observable<BaseHttps<T>>, observer: Observer<BaseHttps<T>>) { observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer) }
BaseHttps类如下,可以根据业务需求更改
/** * @author 新空 * @time 2018/3/26/026 1:49 * @week 星期一 */class BaseHttps<T> : Serializable { var code: Int = 0 @SerializedName("msg") var message: String? = null var data: T? = null }
APi接口如下
/** * Created by Administrator on 2018/3/15. */interface Api { /** * 注册 */ @FormUrlEncoded @POST("user/register") fun register(@FieldMap map: Map<String, String>, @Field("sign") sign: String): Observable<BaseHttps<Any>> }
使用方法
sign(map ) 对map进行签名,需要后台和前端统一
var map = TreeMap<String, String>() map["phone"] = phone map["password"] = password map["code"] = code toSubscribe(RetrofitManager.builder().service.register(map, sign(map)), object : Observer<BaseHttps<Any>> { override fun onCompleted() { } override fun onNext(t: BaseHttps<Any>?) { if (t!!.code == 200) { toast(t.message!!) finish() } } override fun onError(e: Throwable?) { if (e != null) { toast("网络异常") e.printStackTrace() } } })