import com.tencent.qcloud.core.http.HttpLogger;import com.tencent.qcloud.core.http.HttpLoggingInterceptor;import com.tencent.qcloud.core.http.NetworkClient;import com.tencent.qcloud.core.http.NetworkProxy;import com.tencent.qcloud.core.http.QCloudHttpClient;import java.util.concurrent.TimeUnit;import javax.net.ssl.HostnameVerifier;import okhttp3.Dns;import okhttp3.OkHttpClient;/*** Implement a custom network Client here, for example, an OkHttpClient instance.*/public class CustomizeOkHttpNetworkClient extends NetworkClient {private static final String TAG = "CustomizeOkHttpNetworkClient";private OkHttpClient okHttpClient;@Overridepublic void init(QCloudHttpClient.Builder b, HostnameVerifier hostnameVerifier,final Dns dns, HttpLogger httpLogger) {super.init(b, hostnameVerifier, dns, httpLogger);HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(httpLogger);logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(15 * 1000, TimeUnit.MILLISECONDS).readTimeout(30 * 1000, TimeUnit.MILLISECONDS).writeTimeout(30 * 1000, TimeUnit.MILLISECONDS).addInterceptor(logInterceptor);okHttpClient = builder.build();}@Overridepublic NetworkProxy getNetworkProxy() {CustomizeOkHttpNetworkProxy customizeOkHttpNetworkProxy = new CustomizeOkHttpNetworkProxy(okHttpClient);return customizeOkHttpNetworkProxy;}}
import com.tencent.qcloud.core.http.NetworkProxy;import java.io.IOException;import okhttp3.Call;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;/*** Implement a network request here, for example, okhttp's Call.*/public class CustomizeOkHttpNetworkProxy<T> extends NetworkProxy<T> {private static final String TAG = "CustomizeOkHttpNetworkProxy";private Call httpCall;private OkHttpClient okHttpClient;public CustomizeOkHttpNetworkProxy(OkHttpClient okHttpClient){this.okHttpClient = okHttpClient;}@Overridepublic void cancel(){if (httpCall != null) {httpCall.cancel();}}@Overridepublic Response callHttpRequest(Request okHttpRequest) throws IOException {httpCall = okHttpClient.newCall(okHttpRequest);return httpCall.execute();}}
CosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig.Builder(.setRegion(region))// Other configurations remain unchanged..setCustomizeNetworkClient(new CustomizeOkHttpNetworkClient()) // Set the custom network client mentioned above.builder();
import android.util.Log;import com.tencent.qcloud.core.http.HttpLogger;import com.tencent.qcloud.core.http.NetworkClient;import com.tencent.qcloud.core.http.NetworkProxy;import com.tencent.qcloud.core.http.QCloudHttpClient;import java.io.IOException;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import javax.net.ssl.HostnameVerifier;import okhttp3.Dns;import okhttp3.Request;import okio.BufferedSink;import okio.Okio;/*** Implement a custom network Client here.*/public class CustomizeNetworkClient extends NetworkClient {private static final String TAG = "CustomizeNetworkClient";private HttpURLConnectionManager httpURLConnectionManager;@Overridepublic void init(QCloudHttpClient.Builder b, HostnameVerifier hostnameVerifier, Dns dns, HttpLogger httpLogger) {super.init(b, hostnameVerifier, dns, httpLogger);httpURLConnectionManager = new HttpURLConnectionManager();}@Overridepublic NetworkProxy getNetworkProxy() {return new CustomizeNetworkProxy(httpURLConnectionManager);}public static class HttpURLConnectionManager {private static final int CONNECT_TIMEOUT = 5000;private static final int READ_TIMEOUT = 5000;public HttpURLConnection createConnection(Request request) throws IOException {Log.d("NetworkRequest", "Request URL: " + request.url());Log.d("NetworkRequest", "Request method: " + request.method());URL url = new URL(request.url().toString());HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setConnectTimeout(CONNECT_TIMEOUT);connection.setReadTimeout(READ_TIMEOUT);connection.setRequestMethod(request.method());// Set headersfor (String name : request.headers().names()) {connection.setRequestProperty(name, request.header(name));Log.d("NetworkRequest", "Header: " + name + " = " + request.header(name));}// Set bodyif (request.body() != null) {connection.setDoOutput(true);try (OutputStream outputStream = connection.getOutputStream();BufferedSink bufferedSink = Okio.buffer(Okio.sink(outputStream))) {request.body().writeTo(bufferedSink);}Log.d("NetworkRequest", "Request body set");}return connection;}}}
import android.text.TextUtils;import android.util.Log;import com.tencent.qcloud.core.http.NetworkProxy;import java.io.IOException;import java.net.HttpURLConnection;import java.util.List;import java.util.Map;import okhttp3.Headers;import okhttp3.MediaType;import okhttp3.Protocol;import okhttp3.Request;import okhttp3.Response;import okhttp3.ResponseBody;import okio.BufferedSource;import okio.Okio;/*** Implement a network request here.*/public class CustomizeNetworkProxy<T> extends NetworkProxy<T> {private static final String TAG = "CustomizeNetworkProxy";private HttpURLConnection connection;private CustomizeNetworkClient.HttpURLConnectionManager httpURLConnectionManager;public CustomizeNetworkProxy(CustomizeNetworkClient.HttpURLConnectionManager httpURLConnectionManager) {this.httpURLConnectionManager = httpURLConnectionManager;}@Overrideprotected void cancel() {if (connection != null) {connection.disconnect();}}@Overridepublic Response callHttpRequest(Request okHttpRequest) throws IOException {try {connection = httpURLConnectionManager.createConnection(okHttpRequest);int responseCode = connection.getResponseCode();Log.d("NetworkRequest", "Response code: " + responseCode);Response response = convertToOkHttpResponse(connection);Log.d("NetworkRequest", "Response headers: " + response.headers());return response;} catch (IOException e) {Log.e("NetworkRequest", "Failed to execute HTTP request", e);throw e;}}@Overrideprotected void disconnect(){if (connection != null) {connection.disconnect();}}private Response convertToOkHttpResponse(HttpURLConnection connection) throws IOException {int code = connection.getResponseCode();String message = connection.getResponseMessage();String contentType = connection.getContentType();int contentLength = connection.getContentLength();// Convert headersHeaders.Builder headersBuilder = new Headers.Builder();Map<String, List<String>> headerFields = connection.getHeaderFields();for (Map.Entry<String, List<String>> entry : headerFields.entrySet()) {String name = entry.getKey();if (TextUtils.isEmpty(name)) continue;for (String value : entry.getValue()) {headersBuilder.add(name, value);}}Headers headers = headersBuilder.build();// Convert bodyBufferedSource source = Okio.buffer(Okio.source(connection.getInputStream()));ResponseBody body = new ResponseBody() {@Overridepublic MediaType contentType() {if(TextUtils.isEmpty(contentType)){return null;} else {return MediaType.parse(contentType);}}@Overridepublic long contentLength() {return contentLength;}@Overridepublic BufferedSource source() {return source;}};// Build OkHttp ResponseRequest request = new Request.Builder().url(connection.getURL().toString()).build();return new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(code).message(message).headers(headers).body(body).build();}}
CosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig.Builder(.setRegion(region))// Other configurations remain unchanged..setCustomizeNetworkClient(new CustomizeNetworkClient()) // Set the above-mentioned custom network client.builder();
#import <Foundation/Foundation.h>#import "QCloudCustomLoader.h"@interface QCloudAFLoaderTask : QCloudCustomLoaderTask@end
#import "QCloudAFLoaderTask.h"#import "QCloudAFLoaderSession.h"#import "AFNetworking/AFNetworking.h"@interface QCloudAFLoaderTask ()@property (nonatomic,strong)QCloudCustomSession *customSession;@property (nonatomic,strong)NSMutableURLRequest *httpRequest;@property (nonatomic,strong)NSURL *fromFile;@property (nonatomic,strong)NSURLSessionDataTask *task;@end@implementation QCloudAFLoaderTask// Custom task constructor- (instancetype)initWithHTTPRequest:(NSMutableURLRequest *)httpRequestfromFile:(NSURL *)fromFilesession:(QCloudCustomSession *)session{self = [super init];if (self) {self.fromFile = fromFile;self.httpRequest = httpRequest;self.currentRequest = httpRequest;self.originalRequest = httpRequest;self.customSession = session;}return self;}// Custom task startup method. This method is implemented by the business layer and called by the SDK.// Use your own network library to construct and initiate network requests.// The logic for progress callbacks, completion callbacks, etc., calls the corresponding methods of the custom session and passes them to the sdk.-(void)resume{QCloudWeakSelf(self);if (!self.fromFile) {self.task = [[[QCloudAFLoaderSession session] manager] dataTaskWithRequest:self.httpRequest uploadProgress:^(NSProgress * _Nonnull uploadProgress) {QCloudStrongSelf(self);// Handling upload progress callbacks.[strongself.customSession customTask:self didSendBodyData:uploadProgress.completedUnitCount totalBytesSent:uploadProgress.totalUnitCount totalBytesExpectedToSend:uploadProgress.totalUnitCount];} downloadProgress:^(NSProgress * _Nonnull downloadProgress) {QCloudStrongSelf(self);// Handling download progress callbacks.[strongself.customSession customTask:self didSendBodyData:downloadProgress.completedUnitCount totalBytesSent:downloadProgress.totalUnitCount totalBytesExpectedToSend:downloadProgress.totalUnitCount];} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {QCloudStrongSelf(self);// Handling completion progress callbacks.[strongself.customSession customTask:strongself didReceiveResponse:response completionHandler:nil];[strongself.customSession customTask:strongself didReceiveData:responseObject];[strongself.customSession customTask:strongself didCompleteWithError:error];}];// Initiate a request[self.task resume];}else{// Upload local fileself.task = [[[QCloudAFLoaderSession session] manager] uploadTaskWithRequest:self.httpRequest fromFile:self.fromFile progress:^(NSProgress * _Nonnull uploadProgress) {QCloudStrongSelf(self);// Handling upload progress callbacks.[strongself.customSession customTask:strongself didSendBodyData:uploadProgress.completedUnitCount totalBytesSent:uploadProgress.totalUnitCount totalBytesExpectedToSend:uploadProgress.totalUnitCount];} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {QCloudStrongSelf(self);// Handling completion progress callbacks.[strongself.customSession customTask:strongself didReceiveResponse:response completionHandler:nil];[strongself.customSession customTask:strongself didReceiveData:responseObject];[strongself.customSession customTask:strongself didCompleteWithError:error];}];// Initiate a request[self.task resume];}}- (void)cancel{[self.task cancel];}@end
#import <Foundation/Foundation.h>#import "QCloudCore/QCloudCustomLoader.h"#import "AFNetworking/AFNetworking.h"NS_ASSUME_NONNULL_BEGIN@interface QCloudAFLoaderSession : QCloudCustomSession// Custom session holds an instance of AFURLSessionManager.@property (nonatomic,strong)AFURLSessionManager *manager;+(QCloudAFLoaderSession *)session;@end
#import "QCloudAFLoaderSession.h"#import "QCloudAFLoaderTask.h"@implementation QCloudAFLoaderSession// QCloudAFLoaderSession is a singleton+(QCloudAFLoaderSession *)session{static QCloudAFLoaderSession *session = nil;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{session = [[QCloudAFLoaderSession alloc] init];});return session;}- (instancetype)init{self = [super init];if (self) {// Initialize AFURLSessionManager;NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];self.manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];self.manager.responseSerializer = [[AFCompoundResponseSerializer alloc]init];}return self;}-(QCloudCustomLoaderTask *)taskWithRequset:(NSMutableURLRequest *)requestfromFile:(NSURL *)fromFile{QCloudAFLoaderTask * task = [[QCloudAFLoaderTask alloc]initWithHTTPRequest:request fromFile:fromFile session:[QCloudAFLoaderSession session]];return task;}@end
#import <Foundation/Foundation.h>#import "QCloudCustomLoader.h"NS_ASSUME_NONNULL_BEGIN@interface QCloudAFLoader : NSObject <QCloudCustomLoader>@end
#import "QCloudAFLoader.h"#import "QCloudAFLoaderSession.h"@implementation QCloudAFLoader// Holds a custom session instance.-(QCloudCustomSession *)session{return [QCloudAFLoaderSession session];}// Whether to enable the current network layer-(BOOL)enable:(QCloudHTTPRequest *)httpRequest{return YES;}@end
// During project launch, initialize the custom loader and call the addLoader method of [QCloudLoaderManager manager] to register it into the SDK's custom network loader.// Supports adding multiple custom loaders for dynamic switching at the business layer.[[QCloudLoaderManager manager]addLoader: [[QCloudAFLoader alloc]init]];// Enable the custom network layer switch. When this switch is disabled, all custom loaders become unavailable.[QCloudLoaderManager manager].enable = YES;
Feedback