iOS网络请求MKNetworkKit中willSendRequest异常

由于公司用的是MKNetworkKit第三方的网络请求框架,最近在做一个新项目的时候有一个接口是POST一个mobile字段来获取验证码,但是却发现,不论是系统的请求还是AFNETWork都可以正常调用。

系统请求1
//第一步,创建URL

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

//第二步,创建请求

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

[request setHTTPMethod:@"POST"];

NSString*str =@"mobile=***********";//设置参数

NSData*data = [strdataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:data];

//第三步,连接服务器

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

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

[connection start];

系统请求2
//获得NSURLSession对象

NSURLSession*session = [NSURLSessionsharedSession];

//创建请求

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

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

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

//创建任务

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

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

}];

//启动任务

[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];

在缓慢的调试过程中发现,拼接一个完整的URL,POST参数为nil的时候,是可以获取到验证码信息的,那么就把目标放在POST参数上了。

最后发现在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,虽然有不同的参数,但是即使本地设置了也不能生效,此处有疑问,只能单独处理该域名下的请求。

-(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,似乎是后台的接口不支持这种处理。

只用实现了这个block才会有自定义,加上有博文指出这个代理方法的作用是重定向,感觉是MK中有写鸡肋,不过做后台的同事应该在处理上也有问题,特此记录,与君共勉!

你可能感兴趣的:(iOS网络请求MKNetworkKit中willSendRequest异常)