NSMutableURLRequest实现Post访问

来源:互联网 发布:软件评测中心招聘 编辑:程序博客网 时间:2024/06/06 01:58
-(void)PostStr{    NSString *urlstring= @"http://116.255.130.35:11011/api/TestApi2/PostFromPosData";    NSString *poststr=@"hellowrold";        NSURL *url=[NSURL URLWithString:urlstring];    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];    [request setHTTPMethod:@"POST"];    [request setHTTPBody:[poststr dataUsingEncoding:NSUTF8StringEncoding]];    NSOperationQueue *queue=[[NSOperationQueue alloc] init];    [NSURLConnection sendAsynchronousRequest:request queue:queue                           completionHandler:^(NSURLResponse *respone,                                               NSData *data,                                               NSError *error)     {         if ([data length]>0 && error==nil) {             NSString *jsonstring=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];             NSLog(jsonstring);         }              }     ];}

原创粉丝点击