iOS : 互联网连接可用但网络服务调用出现超时错误,相同的网络服务正在 android
iOS : Internet connection is available but getting time out error in web service call , same web services are working in android
这是我的网络服务调用。如果我连续调用网络服务,一段时间后开始出现超时错误,但互联网可用。
相同的 Web 服务在 android
中正常工作
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request.timeoutInterval = 30;
request.URL = [NSURL URLWithString:finalURL];
request.HTTPMethod = @"POST";
[request setValue:@"123456" forHTTPHeaderField:@"header"];
if (parameters != nil) {
NSMutableData * body = [[NSMutableData alloc] init];
NSString *boundary = @"---------------------------V2ymHFg03ehbqgZCaKO6jy";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
for (id key in parameters) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@", [parameters objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
}
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:body];
}
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
// This will get the NSURLResponse into NSHTTPURLResponse format
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if (httpResponse.statusCode == 200) {
NSMutableDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog( @"Response = %@",JSON);
});
} else {
NSLog( @"Error = %@",error);
}
}];
试试这个:
NSString *strServicePath = @"YourURLString";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strServicePath]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSDictionary *_params = [NSDictionary dictionaryWithObjectsAndKeys:yourValue,@"yourKey",nil];
// post body
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSString *param in _params) {
NSMutableString *strPart = [NSMutableString stringWithFormat:@"%@",[self createPartWithKey:param value:[_params objectForKey:param] boundary:boundary encoding:@"8bit" contentType:@"text/plain" contentFilename:@""]];
[body appendData:[strPart dataUsingEncoding:NSUTF8StringEncoding]];
}
//You can give your own strProfileImageName
NSString *strProfileImageName = [NSString stringWithFormat:@"%@_%@_.jpg",@"some_pic",_userID];
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@\r\n", @"some_pic",strProfileImageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue: [NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//Your Code Here
}];
尝试在这实现。希望这有帮助。
您似乎访问了错误的数组。
for (id key in parameters) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@", [parameters objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
}
上面代码中的参数似乎是一个字符串数组而不是字典。
您可以改用下面的代码。
[parameters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj,BOOL *_Nonnull stop) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@", obj] dataUsingEncoding:NSUTF8StringEncoding]];
}];
这是我的网络服务调用。如果我连续调用网络服务,一段时间后开始出现超时错误,但互联网可用。
相同的 Web 服务在 android
中正常工作NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request.timeoutInterval = 30;
request.URL = [NSURL URLWithString:finalURL];
request.HTTPMethod = @"POST";
[request setValue:@"123456" forHTTPHeaderField:@"header"];
if (parameters != nil) {
NSMutableData * body = [[NSMutableData alloc] init];
NSString *boundary = @"---------------------------V2ymHFg03ehbqgZCaKO6jy";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
for (id key in parameters) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@", [parameters objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
}
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:body];
}
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
// This will get the NSURLResponse into NSHTTPURLResponse format
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if (httpResponse.statusCode == 200) {
NSMutableDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog( @"Response = %@",JSON);
});
} else {
NSLog( @"Error = %@",error);
}
}];
试试这个:
NSString *strServicePath = @"YourURLString";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strServicePath]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSDictionary *_params = [NSDictionary dictionaryWithObjectsAndKeys:yourValue,@"yourKey",nil];
// post body
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSString *param in _params) {
NSMutableString *strPart = [NSMutableString stringWithFormat:@"%@",[self createPartWithKey:param value:[_params objectForKey:param] boundary:boundary encoding:@"8bit" contentType:@"text/plain" contentFilename:@""]];
[body appendData:[strPart dataUsingEncoding:NSUTF8StringEncoding]];
}
//You can give your own strProfileImageName
NSString *strProfileImageName = [NSString stringWithFormat:@"%@_%@_.jpg",@"some_pic",_userID];
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@\r\n", @"some_pic",strProfileImageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue: [NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//Your Code Here
}];
尝试在这实现。希望这有帮助。
您似乎访问了错误的数组。
for (id key in parameters) { [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@", [parameters objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]]; }
上面代码中的参数似乎是一个字符串数组而不是字典。
您可以改用下面的代码。
[parameters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj,BOOL *_Nonnull stop) { [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@", obj] dataUsingEncoding:NSUTF8StringEncoding]]; }];