Created
August 12, 2020 14:53
-
-
Save hhhaiai/476e106b4ee48bba861ebc15e2f74556 to your computer and use it in GitHub Desktop.
微信文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.graphics.Bitmap; | |
| import android.net.http.SslError; | |
| import android.os.Build; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.view.View; | |
| import android.webkit.CookieManager; | |
| import android.webkit.CookieSyncManager; | |
| import android.webkit.JsResult; | |
| import android.webkit.SslErrorHandler; | |
| import android.webkit.WebChromeClient; | |
| import android.webkit.WebResourceError; | |
| import android.webkit.WebResourceRequest; | |
| import android.webkit.WebResourceResponse; | |
| import android.webkit.WebSettings; | |
| import android.webkit.WebView; | |
| import android.webkit.WebViewClient; | |
| import com.wvd.R; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| public class WechatContentActivity extends Activity { | |
| private String TAG = "sanbo"; | |
| private Map<String, String> headers = null; | |
| private WebView webView = null; | |
| // private static final String content = "https://mp.weixin.qq.com/s/riqWTnMKUq9VuXFpcxrb5w"; | |
| private static final String content = "http://nhcx.spgnovl.cn/bqlsakii/yyhchfzmixgefmkv/1043"; | |
| private static final String audio = "https://mp.weixin.qq.com/s/zv_l5hcwyxUlHvdWFK4Enw"; | |
| private static final String video = "https://mp.weixin.qq.com/s/E-uqITBTODNGo6WD8kokWg"; | |
| private String ua = "mozilla/5.0 (linux; android 5.1.1; mi note pro build/lmy47v) applewebkit/537.36 (khtml, like gecko) version/4.0 chrome/37.0.0.0 mobile mqqbrowser/6.2 tbs/036215 safari/537.36 micromessenger/6.3.16.49_r03ae324.780 nettype/wifi language/zh_cn"; | |
| // private String ua1 ="MicroMessenger Client"; | |
| // private String ua ="mozilla/5.0 (linux; u; android 4.1.2; zh-cn; mi-one plus build/jzo54k) applewebkit/534.30 (khtml, like gecko) version/4.0 mobile safari/534.30 micromessenger/5.0.1.352"; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_wechat_content); | |
| init(this); | |
| } | |
| public void init(Context ctx) { | |
| webView = findViewById(R.id.wvWechats); | |
| if (webView != null) { | |
| cleanCookie(ctx); | |
| //声明WebSettings子类 | |
| WebSettings webSettings = webView.getSettings(); | |
| webSettings.setAllowContentAccess(true); | |
| webSettings.setBlockNetworkLoads(false); | |
| webSettings.setJavaScriptEnabled(true);//启用js | |
| webSettings.setBlockNetworkImage(false);//解决图片不显示 | |
| webSettings.setJavaScriptCanOpenWindowsAutomatically(true); | |
| webSettings.setDomStorageEnabled(true); | |
| webSettings.setGeolocationEnabled(true); | |
| webSettings.setPluginState(WebSettings.PluginState.ON); | |
| webSettings.setSupportMultipleWindows(true); | |
| webSettings.setAppCacheEnabled(true); | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
| webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); | |
| } | |
| //如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript | |
| webSettings.setJavaScriptEnabled(true); | |
| //设置自适应屏幕,两者合用 | |
| webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小 | |
| webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小 | |
| //缩放操作 | |
| webSettings.setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。 | |
| webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false,则该WebView不可缩放 | |
| webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件 | |
| //其他细节操作 | |
| webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存 | |
| webSettings.setAllowFileAccess(true); //设置可以访问文件 | |
| webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口 | |
| webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片 | |
| webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式 | |
| //优先使用缓存: | |
| webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); | |
| //缓存模式如下: | |
| //LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据 | |
| //LOAD_DEFAULT: (默认)根据cache-control决定是否从网络上取数据。 | |
| //LOAD_NO_CACHE: 不使用缓存,只从网络获取数据. | |
| //LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。 | |
| // | |
| // //不使用缓存: | |
| // webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); | |
| // if (NetStatusUtil.isConnected(getApplicationContext())) { | |
| // webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);//根据cache-control决定是否从网络上取数据。 | |
| // } else { | |
| // webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没网,则从本地获取,即离线加载 | |
| // } | |
| // | |
| // webSettings.setDomStorageEnabled(true); // 开启 DOM storage API 功能 | |
| // webSettings.setDatabaseEnabled(true); //开启 database storage API 功能 | |
| // webSettings.setAppCacheEnabled(true);//开启 Application Caches 功能 | |
| // | |
| // String cacheDirPath = getFilesDir().getAbsolutePath() + APP_CACAHE_DIRNAME; | |
| // webSettings.setAppCachePath(cacheDirPath); //设置 Application Caches 缓存目录 | |
| logi("UA1: " + webSettings.getUserAgentString()); | |
| webView.setWebViewClient(new WebViewClient() { | |
| @Override | |
| public void onPageStarted(WebView view, String url, Bitmap favicon) { | |
| super.onPageStarted(view, url, favicon); | |
| logi("onPageStarted URL:" + url); | |
| } | |
| @Override | |
| public void onPageFinished(WebView view, String url) { | |
| super.onPageFinished(view, url); | |
| logi("onPageFinished URL:" + url); | |
| } | |
| @Override | |
| public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
| view.loadUrl(url); | |
| logi("shouldOverrideUrlLoading URL:" + url); | |
| return true; | |
| } | |
| @Override | |
| public void onLoadResource(WebView view, String url) { | |
| //设定加载资源的操作 | |
| logi("onLoadResource URL:" + url); | |
| } | |
| @Override | |
| public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { | |
| super.onReceivedError(view, request, error); | |
| logi("onReceivedError "); | |
| } | |
| @Override | |
| public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { | |
| super.onReceivedHttpError(view, request, errorResponse); | |
| logi("onReceivedHttpError "); | |
| } | |
| @Override | |
| public final void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) { | |
| logi("onReceivedSslError sslErrorHandler: " + sslErrorHandler + " ; sslError: " + sslError); | |
| sslErrorHandler.proceed(); | |
| } | |
| @Override | |
| public WebResourceResponse shouldInterceptRequest(WebView view, String url) { | |
| logi("shouldInterceptRequest url: " + url); | |
| return super.shouldInterceptRequest(view, url); | |
| } | |
| @Override | |
| public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { | |
| super.onReceivedError(view, errorCode, description, failingUrl); | |
| logi("onReceivedError,11111 errorCode: " + errorCode + ",description:" + description + ",failingUrl:" + failingUrl); | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| return; | |
| } | |
| logi("will do ERROR...11111..."); | |
| } | |
| }); | |
| webView.setWebChromeClient(new WebChromeClient() { | |
| @Override | |
| public boolean onJsAlert(WebView view, String url, String message, JsResult result) { | |
| logi("onJsAlert URL:" + url); | |
| return super.onJsAlert(view, url, message, result); | |
| } | |
| }); | |
| logi("buildView webview will set client over. will clear cookie. "); | |
| webView.requestFocus(); | |
| webView.requestFocusFromTouch(); | |
| logi("WechatWvImpl will load url "); | |
| headers = new HashMap<>(); | |
| headers.put("Cache-Control", "no-cache, no-store, must-revalidate"); | |
| headers.put("Expires", "0"); | |
| headers.put("Accept", "*/*"); | |
| headers.put("Connection", "Keep-Alive"); | |
| headers.put("Content-Type", "application/octet-stream"); | |
| // headers.put("Host", "extshort.weixin.qq.com"); | |
| // headers.put("Upgrade", "mmtls"); | |
| headers.put("User-Agent", ua); | |
| headers.put("Content-Security-Policy", ""); | |
| headers.put("Content-Security-Policy-Report-Only", ""); | |
| webSettings.setUserAgentString(ua); | |
| // webView.loadUrl(url, headers); | |
| } else { | |
| logi("the webview is null!"); | |
| } | |
| } | |
| private void logi(String logs) { | |
| Log.i(TAG, logs); | |
| } | |
| private static void cleanCookie(Context ctx) { | |
| //webView清除Cookie | |
| CookieSyncManager.createInstance(ctx); | |
| CookieManager.getInstance().removeAllCookie(); | |
| CookieSyncManager.getInstance().sync(); | |
| } | |
| public void onClick(View view) { | |
| switch (view.getId()) { | |
| case R.id.btnWeAudio: | |
| webView.loadUrl(audio, headers); | |
| break; | |
| case R.id.btnWeVideo: | |
| webView.loadUrl(video, headers); | |
| break; | |
| case R.id.btnWeContent: | |
| webView.loadUrl(content, headers); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment