AFNetworking类库使用
AFNetworking都是异步请求。
GET请求
NSString *url =URL;
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.requestSerializer = [AFJSONRequestSerializerserializer];
manager.responseSerializer = [AFHTTPResponseSerializerserializer];//这个决定了下面responseObject返回的类型
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"application/json"];
//3.发送请求
[manager GET:url
parameters:nil
success: ^(AFHTTPRequestOperation *operation,id responseObject) {
NSData *data = responseObject;
}
failure: ^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog(@"%@", error);
}];
因为AFHTTPRequestOperationManager内调用的是AFHTTPRequestSerializer的接口,也可直接使用下方法:
AFHTTPRequestSerializer *request = [AFHTTPRequestSerializernew];
[request setValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
NSMutableURLRequest *requestGet = [requestrequestWithMethod:@"GET"URLString:url parameters:nilerror:nil];
NSData *data = [NSURLConnectionsendSynchronousRequest:requestGetreturningResponse:nilerror:nil];
NSDictionary *resdict = [NSJSONSerializationJSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
POST请求
NSString *requestUrl = URL;
requestUrl = [requestUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *requestData = [NSMutableDictionary dictionaryWithCapacity:XX];
...
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.requestSerializer = [AFJSONRequestSerializerserializer];
manager.responseSerializer = [AFHTTPResponseSerializerserializer];//这个决定了下面responseObject返回的类型
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"application/json"];
//3.发送请求
[managerPOST:requestUrl
parameters:requestData
success: ^(AFHTTPRequestOperation *operation,id responseObject) {
NSData *data = responseObject;
}
failure: ^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog(@"%@", error);
}];
ASIHTTPRequest类库使用
同步GET请求
NSURL *dataURL=[NSURLURLWithString:url];
ASIHTTPRequest *requestASI = [ASIHTTPRequestrequestWithURL:dataURL];
[requestASI setTimeOutSeconds:1.0f]; //设置请求超时时间
[requestASI addRequestHeader:@"Content-Type"value:@"application/json"];
[requestASI setRequestMethod:@"GET"];
[requestASI setPostBody:nil];
[requestASI startSynchronous];
NSError *error = [requestASIerror];
if (!error)
{
NSData *data = [requestASI responseData];
NSDictionary *resdict = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];
}
同步POST请求
NSMutableDictionary *requestData = [NSMutableDictionary dictionaryWithCapacity:XX];
...
NSData *postData = [NSJSONSerializationdataWithJSONObject:requestData
options:NSJSONWritingPrettyPrinted
error: nil];
NSMutableData *tempJsonData = [NSMutableDatadataWithData:postData];
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:dataURL];
[request addRequestHeader:@"Content-Type"value:@"application/json"];
[request setTimeOutSeconds:1.0f];
[requestsetRequestMethod:@"POST"];
[requestsetPostBody:tempJsonData];
[request startSynchronous];
NSError *error = [requesterror];
if (!error)
{
NSData *data = [request responseData];
NSDictionary *resdict = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];
}
GET同步请求
NSMutableURLRequest*request = [NSMutableURLRequest new];
[request setURL:dataURL];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableContainers error:nil];
POST同步请求
NSMutableDictionary *requestData = [NSMutableDictionary dictionaryWithCapacity:XX];
...
NSData *postData = [NSJSONSerialization dataWithJSONObject:requestData
options:NSJSONWritingPrettyPrinted
error: nil];
NSMutableURLRequest *requestPOST = [NSMutableURLRequest new];
[requestPOSTsetURL:dataURL];
[requestPOSTsetHTTPMethod:@"POST"];
[requestPOST setValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
[requestPOSTsetHTTPBody:postData];
NSData *received = [NSURLConnectionsendSynchronousRequest:requestPOST returningResponse:nilerror:nil];
NSDictionary *resDict = [NSJSONSerializationJSONObjectWithData:received options:NSJSONReadingMutableContainerserror:nil];