0
我正在使用NSURLConnection方法调用Web服务,我希望将其替换为第三方库AFNetworking我应该如何实现这一点我正在使用数据调用Web服务这里的时间就是我目前所做的。在AFNetworking的帮助下调用Web服务目标C
- (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是我喜欢的用户名和密码进行加密的数据。 在此先感谢。
我的数据是简单TextPlain –
无所谓。您应该使用上面的代码将数据传递给Web服务。你的数据可以是图像(NSData)或者只是一个纯字符串。你可以使用这段代码。 –