objective-c使用post方式向服务器传数据

来源:互联网 发布:松下网络摄像机官网 编辑:程序博客网 时间:2024/06/07 01:33

(转自http://blog.sina.com.cn/s/blog_667126df01014h10.html)


-(void)postData{   

 NSString * urlStr = [NSString stringWithFormat:@"xxxxxxxxxxx"];

    NSURL * url = [NSURL URLWithString:urlStr];

    NSString *body = [NSString stringWithFormat:@"A=%@&B=%@&C=%@&D=%@",strA,strB,strC,strD];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    

    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:[body dataUsingEncoding: NSASCIIStringEncoding]];

    

    NSURLConnection *theConnection=[[[NSURLConnection alloc] initWithRequest:request delegate:self]autorelease];

    if (theConnection) {

        self.mutabledata=[[NSMutableData data] retain];//如果链接上了

    }

}

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [self.mutabledata appendData:data];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

NSLog(@"postData finished");

}


0 0
原创粉丝点击