本文主要介绍如何在业务应用中开启上传网络请求代理,您可通过开启上传网络请求代理,使开发者可以根据具体业务需要自行实现上传网络请求。
您需要通过以下两步完成上传网络请求代理的配置。
通过NetworkProxy.setListener
方法设置网络请求的回调监听器,具体如下所示。
NetworkProxy.setListener(new NetworkProxy.NetworkProxyListener() { /** * 需要进行网络请求任务回调 * @param proxyHandle 网络请求任务 c++ 指针 * @param request 请求信息 * @return 响应的结果返回 SDK */ @Override public NetworkProxyResponse request(long proxyHandle, NetworkProxyRequest request) { } /** * 网络任务停止回调 * @param proxyHandle 网络请求任务 c++ 指针。 */ @Override public void stop(long proxyHandle) { } public class NetworkProxyRequest { // 请求方法,"GET" 或 "POST" public String method; // 请求的 url 地址 public String url; // 请求的 header 信息 public HashMap<String, String> headers; // 请求体,实际上传文件数据时使用 public byte[] body; // 若此成员值不为空,则表示请求体是字符串,优先使用此值 public String body_str; } public class NetworkProxyResponse { // 响应的 httpcode public int code; // 响应的 header 信息 public String header; // 响应的 body 内容 public String body; // 解析到的 ip 列表,可选,用于信息记录 public String ip_list; // 网络请求失败时的错误信息 public String error_msg; }
对实例开启网络代理,具体如下所示。
mUploader.setNetworkType(KeyIsMainNetworkType, NetworkTypeProxy);
具体 Demo 示例如下所示。
// 1. 全局监听网络请求回调 HashMap<Long, NetworkProxyClient> proxyClients = new HashMap<>(); NetworkProxy.setListener(new NetworkProxy.NetworkProxyListener() { @Override public NetworkProxyResponse request(long proxyHandle, NetworkProxyRequest request) { // 收到请求回调,创建 NetworkProxyClient 实例来管理请求,并将其存入 proxyClients NetworkProxyClient networkProxyClient = new NetworkProxyClient(); proxyClients.put(proxyHandle, networkProxyClient); // 收到请求回调,调用 NetworkProxyClient 的 request 方法进行网络请求,并返回响应结果 return networkProxyClient.request(proxyHandle, request); } @Override public void stop(long proxyHandle) { // 收到停止请求回调,停止对应实例请求 NetworkProxyClient networkProxyClient = proxyClients.get(proxyHandle); if (networkProxyClient != null) { networkProxyClient.stop(); } proxyClients.remove(proxyHandle); } }); // 2. 对实例开启网络代理 mUploader.setNetworkType(KeyIsMainNetworkType, NetworkTypeProxy); public class NetworkProxyClient { private static final String TAG = "ttmn NetworkProxyClient"; private Call call; public NetworkProxyResponse request(long proxyHandle, NetworkProxyRequest networkRequest) { // 获取请求方法、URL、头部信息、请求体和请求体字符串 String requestMethod = networkRequest.method; String requestUrl = networkRequest.url; HashMap<String, String> requestHeaders = networkRequest.headers; byte[] requestBody = networkRequest.body; String requestBodyStr = networkRequest.body_str; // 创建 OkHttpClient 实例 OkHttpClient httpClient = new OkHttpClient.Builder() .protocols(Collections.singletonList(Protocol.HTTP_1_1)) .build(); Request.Builder builder = new Request.Builder().url(requestUrl); // 遍历请求头信息 for (String key : requestHeaders.keySet()) { String value = requestHeaders.get(key); if (value != null) { builder.addHeader(key, value); } } if (TextUtils.equals(requestMethod, HTTP_METHOD_POST)) { // 处理 post 请求 RequestBody finalRequestBody; if (TextUtils.isEmpty(requestBodyStr)) { // 请求体是字符串,若此值不为空,优先此字符串作为请求体 finalRequestBody = RequestBody.create(requestBody); } else { // 请求体是二进制流 String mediaType = "application/x-www-form-urlencoded"; String contentType = requestHeaders.get("Content-Type"); if (!TextUtils.isEmpty(contentType)) { mediaType = contentType; } finalRequestBody = RequestBody.create(requestBodyStr, MediaType.get(mediaType)); } builder.post(finalRequestBody); } else if (TextUtils.equals(requestMethod, HTTP_METHOD_GET)) { // 处理 get 请求 builder.get(); } Request request = builder.build(); call = httpClient.newCall(request); NetworkProxyResponse netResponse = new NetworkProxyResponse(); try { Response response = call.execute(); int code = response.code(); Headers responseHeaders = response.headers(); ResponseBody responseBody = response.body(); JSONObject responseHeaderJson = new JSONObject(); for (String name : responseHeaders.names()) { try { responseHeaderJson.put(name, responseHeaders.get(name)); } catch (JSONException e) { Log.d(TAG, "json error " + e); } } String responseBodyStr = ""; if (responseBody != null) { responseBodyStr = responseBody.string(); } netResponse.code = code; netResponse.header = responseHeaderJson.toString(); netResponse.body = responseBodyStr; } catch (IOException e) { netResponse.error_msg = e.toString(); } return netResponse; } public void stop() { if (call != null) { call.cancel(); } } }