【发布时间】:2017-07-23 05:56:36
【问题描述】:
我正在使用 NSURLConnection 方法调用 Web 服务我想要的是用 3rd 方库 AFNetworking 替换它我应该如何实现这一点我第一次用数据调用 Web 服务这是我目前正在做的事情。
- (void)viewDidLoad {
[super viewDidLoad];
NSURL * linkUrl = [NSURL URLWithString:@URLBase];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:linkUrl];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[str length]];
[theRequest addValue: @"text/plain; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [str dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData alloc] init];
}
else
{
NSLog(@"theConnection is NULL");
}
}
#pragma mark -- Connection Delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//NSLog(@"%@",response);
//[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"\nERROR with theConenction");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseDataString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
NSLog(@"Converted NSData %@ ",responseDataString);
}
这里
[theRequest setHTTPBody: [str dataUsingEncoding:NSUTF8StringEncoding]];
str 是我的加密数据,例如用户名和密码。 提前谢谢!
【问题讨论】:
标签: ios objective-c afnetworking