iOS網(wǎng)絡(luò)請求MKNetworkKit中willSendRequest異常

由于公司用的是MKNetworkKit第三方的網(wǎng)絡(luò)請求框架,最近在做一個新項目的時候有一個接口是POST一個mobile字段來獲取驗證碼,但是卻發(fā)現(xiàn),不論是系統(tǒng)的請求還是AFNETWork都可以正常調(diào)用。

系統(tǒng)請求1
//第一步,創(chuàng)建URL

NSURL*url = [NSURLURLWithString:@"https://tms.beta.ule.com/deliveryApp/router/rest?method=delivery.deliverystaff.sendsmscode"];

//第二步,創(chuàng)建請求

NSMutableURLRequest*request = [[NSMutableURLRequestalloc]initWithURL:urlcachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10];

[request setHTTPMethod:@"POST"];

NSString*str =@"mobile=***********";//設(shè)置參數(shù)

NSData*data = [strdataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:data];

//第三步,連接服務(wù)器

NSURLConnection*connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

NSLog(@"%@",request.allHTTPHeaderFields);

[connection start];

系統(tǒng)請求2
//獲得NSURLSession對象

NSURLSession*session = [NSURLSessionsharedSession];

//創(chuàng)建請求

NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:@"https://tms.beta.ule.com/deliveryApp/router/rest"]];

request.HTTPMethod =@"POST";//請求方法

request.HTTPBody = [@"method=delivery.deliverystaff.sendsmscode&mobile=***********"dataUsingEncoding:NSUTF8StringEncoding];//請求體

//創(chuàng)建任務(wù)

NSURLSessionDataTask*task = [sessiondataTaskWithRequest:requestcompletionHandler:^(NSData*data,NSURLResponse*response,NSError*error) {

NSLog(@"%@", [NSJSONSerializationJSONObjectWithData:dataoptions:kNilOptionserror:nil]);

}];

//啟動任務(wù)

[task resume];

MKNetworkKit
UleMK_MKNetworkOperation*op = [[UleMK_MKNetworkOperationalloc]initWithURLString:@"https://tms.beta.ule.com/deliveryApp/router/rest?method=delivery.deliverystaff.sendsmscode"params:nilhttpMethod:@"POST"];

[op addParams:@{@"mobile":@"***********"}];

[op addCompletionHandler:^(UleMK_MKNetworkOperation*completedOperation) {

NSLog(@"__%@___",[completedOperationresponseString]);

} errorHandler:^(UleMK_MKNetworkOperation*completedOperation,NSError*error) {

NSLog(@"__%@__",error);

}];

[op start];

UleMK_MKNetworkEngine* engine = [[UleMK_MKNetworkEnginealloc]initWithHostName:@"tms.beta.ule.com"];

UleMK_MKNetworkOperation*op = [engine operationWithPath:@"deliveryApp/router/rest?method=delivery.deliverystaff.sendsmscode"params:@{@"mobile":@"***********"}httpMethod:@"POST"ssl:YES];

[op addCompletionHandler:^(UleMK_MKNetworkOperation*completedOperation) {

NSLog(@"__%@___",[completedOperationresponseString]);

} errorHandler:^(UleMK_MKNetworkOperation*completedOperation,NSError*error) {

NSLog(@"__%@__",error);

}];

[engine enqueueOperation: op];

在緩慢的調(diào)試過程中發(fā)現(xiàn),拼接一個完整的URL,POST參數(shù)為nil的時候,是可以獲取到驗證碼信息的,那么就把目標放在POST參數(shù)上了。

最后發(fā)現(xiàn)在MKNetworkKit中有一個

//http://stackoverflow.com/questions/1446509/handling-redirects-correctly-with-nsurlconnection

- (NSURLRequest*)connection: (NSURLConnection*)inConnection

willSendRequest: (NSURLRequest*)inRequest

redirectResponse: (NSURLResponse*)inRedirectResponse;

{

// *******start

// yushengyang 20160316 tms.beta.ule.com不支持重定向

if([inRequest.URL.absoluteStringrangeOfString:@"tms.beta.ule.com"].location!=NSNotFound|| [inRequest.URL.absoluteStringrangeOfString:@"tms.ule.com"].location!=NSNotFound) {

returninRequest;

}

// *******end

NSMutableURLRequest*r = [self.requestmutableCopy];

if(inRedirectResponse) {

[rsetURL: [inRequestURL]];

}else{

// Note that we need to configure the Accept-Language header this late in processing

// because NSURLRequest adds a default Accept-Language header late in the day, so we

// have to undo that here.

// For discussion see:

//http://lists.apple.com/archives/macnetworkprog/2009/Sep/msg00022.html

//http://stackoverflow.com/questions/5695914/nsurlrequest-where-an-app-can-find-the-default-headers-for-http-request

NSString* accept_language =self.shouldSendAcceptLanguageHeader? [selflanguagesFromLocale] :nil;

[rsetValue:accept_languageforHTTPHeaderField:@"Accept-Language"];

}

returnr;

}

在粗體標記中,第一個是我加的代碼,因為這個問題可能做后段接口的同事引起的,所以我只屏蔽該接口。第二個是框架寫的,這里不太明白為何每次都要重新弄一個request,雖然該request就是代理中的,但是不知道為何,只要用的不是inRequest而是self.request通過copy而來的,都會導致收不到驗證碼。

看了看AFNetwork中的處理

- (NSURLRequest*)connection:(NSURLConnection*)connection

willSendRequest:(NSURLRequest*)request

redirectResponse:(NSURLResponse*)redirectResponse

{

if(self.redirectResponse) {

returnself.redirectResponse(connection, request, redirectResponse);

}else{

returnrequest;

}


po self.request.allHTTPHeaderFields

{

}

po inRequest.allHTTPHeaderFields

{

Accept = "*/*";

"Accept-Encoding" = "gzip, deflate";

"Accept-Language" = "en-us";

}

對比了本地self.request和inRequest,雖然有不同的參數(shù),但是即使本地設(shè)置了也不能生效,此處有疑問,只能單獨處理該域名下的請求。

-(NSURLRequest*)connection:(NSURLConnection*)connection

willSendRequest:(NSURLRequest*)request

redirectResponse:(NSURLResponse*)redirectResponse;

{

if(redirectResponse){

// we don't use the new request built for us, except for the URL

NSURL*newURL=[request URL];

// Previously, store the original request in _originalRequest.

// We rely on that here!

NSMutableURLRequest*newRequest=[_originalRequest mutableCopy];

[newRequest setURL:newURL];

returnnewRequest;

}else{

returnrequest;

}

}

查找到其他博文提到的代碼,我代碼里的redirectResponse是nil,似乎是后臺的接口不支持這種處理。

只用實現(xiàn)了這個block才會有自定義,加上有博文指出這個代理方法的作用是重定向,感覺是MK中有寫雞肋,不過做后臺的同事應(yīng)該在處理上也有問題,特此記錄,與君共勉!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容