一些post
-(void)netWork3
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *urlString = @"http://apitest.dnbiz.cn/v1/brand/search";
NSString *str = @"keyword=马力&search_type=1";//设置参数
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", nil];
[manager POST:urlString parameters:str success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"有错误%@", error);
}];
}
-(void)netWorkRequre
{
//同步POST请求
//第一步,创建URL
NSURL *url = [NSURL URLWithString:@"http://apitest.dnbiz.cn/v1/brand/search"];
//第二步,创建请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setHTTPMethod:@"POST"];//设置请求方式为POST,默认为GET
NSString *str = @"keyword=马力&search_type=1";//设置参数
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
//第三步,连接服务器
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSMutableDictionary *mdic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableContainers error:nil];
// NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
// NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
// NSLog(@"%@", str1);
// NSLog(@"请求到的字典%@", mdic);
// NSLog(@"请求");
// NSLog(@"%@", dataDic);
/*
NSData *JSONData = [str1 dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@", responseJSON);
*/
NSDictionary *tempDic = mdic[@"data"];
// NSLog(@"取出来的字典%@", tempDic);
NSSet *set = tempDic[@"result"];
// NSLog(@"%@", set);
for (NSMutableDictionary *infoDic in set) {
// NSLog(@"%@", infoDic);
NSLog(@"%@", [NSString stringWithFormat:@"%@", infoDic[@"addressCn"]]);
}
}
-(void)postAsync
{
//第一步,创建url
NSURL *url = [NSURL URLWithString:@"http://apitest.dnbiz.cn/v1/brand/search"];
//第二步,创建请求
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setHTTPMethod:@"POST"];
NSString *str = @"keyword=马力&search_type=1";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
//第三步,连接服务器
NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
// NSLog(@"%@",[res allHeaderFields]);
self.receivedData = [NSMutableData data];
}
//接收到服务器传输数据的时候调用,此方法根据数据大小执行若干次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.receivedData appendData:data];
}
//数据传完之后调用此方法
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *receiveStr = [[NSString alloc]initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSLog(@"接收到的字符串");
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.receivedData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
NSLog(@"json解析失败:%@",err);
}
NSLog(@"请求过来的数据%@", dic);
}
//网络请求过程中,出现任何错误(断网,连接超时等)会进入此方法
-(void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// NSLog(@"出现问题了%@",[error localizedDescription]);
}
//*******************************************************************//
-(void)netWork
{
NSString *post = [NSString stringWithFormat:@"access_token=U08PrZzS3O3LJQOWZcPx&page=1"];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
// NSString *postLength = [NSString stringWithFormat:@"%ld", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://apitest.dnbiz.cn/v1/service/cloud"]];
[request setHTTPMethod:@"POST"];
// [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//定义
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
//同步提交:POST提交并等待返回值(同步),返回值是NSData类型。
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
//将NSData类型的返回值转换成NSString类型
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"收到字符串信息了:%@",result);
}