如何使用nsurlconnection发送post请求

时间:2012-02-22 00:52:27

标签: iphone ios nsurlconnection asihttprequest nsurlrequest

我已经更改为NSURLConnection和NSURLRequest委托以进行数据库连接等。 我正在使用ASIHTTPRequest库来完成所有这些工作,但最终决定继续进行,因为缺少对第三方库的支持。

我想知道的是如何将发布请求发送到我的数据库,就像使用ASIFormDataRequest一样,如下所示

//This sets up all other request
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

        [request setDelegate:self];
        [request setValidatesSecureCertificate:NO];
        [request setPostValue:@"ClientDataSet.xml" forKey:@"filename"];  
        [request startSynchronous];

即。怎么做用NSURLRequest类发送setPostValues? 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:5)

使用NSURLRequest比使用ASIHTTPRequest稍微困难一些。你必须建立自己的帖子。

NSData *postBodyData = [NSData dataWithBytes: [postBodyString UTF8String] length:[postBodyString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:yourURL]; 
[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:postBodyData];
相关问题