iOS中的HTTP POST请求
嗨,我是新的iOS和我没有发送任何电话到现在,直到现在,我已经尝试了下面的代码
-(void)sendRequest { NSString *vali = @"$uppl!3r$"; NSString *post = [NSString stringWithFormat:@"key1=%@",vali]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSLog(@"%@",postLength); NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:@"http://www.ddemo3.enerjinet.com/webservices/ios/suppliers.php"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (theConnection) { webData = [[NSMutableData data] retain]; NSLog(@"%@",webData); } else { } } -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [webData setLength: 0]; } -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [webData appendData:data]; } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [connection release]; [webData release]; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length: [webData length] encoding:NSUTF8StringEncoding]; NSLog(@"%@",loginStatus); //greeting.text = loginStatus; [loginStatus release]; [connection release]; [webData release]; }
它应该返回我76arrays的logging,但它返回我<>
任何人都可以请帮助我? Web服务已经准备就绪,我需要得到数组的响应,并显示在我的表格视图,请帮助我这样做
一些想法:
-
你
NSLog
你的webData
,它将始终显示<>
(因为你实例化它后立即logging)。 我不知道你为什么要logging。问题是你是否看到
<>
,或者NSLog
中的connectionDidFinishLoading
。 -
我问,因为你没有logging
error
connection:didFailWithError:
如果失败,你永远不知道为什么。 你真的应该在connection:didFailWithError:
loggingerror
connection:didFailWithError:
所以你知道它是否失败,如果是这样,为什么:NSLog(@"%s: %@", __FUNCTION__, error);
-
在你的
connection:didReceiveResponse:
你真的应该看看HTTP状态代码 :if ([response isKindOfClass:[NSHTTPURLResponse class]]) { NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; if (statusCode != 200) NSLog(@"%s: status code is %d; should be 200", __FUNCTION__, statusCode); }
如果不是200,你真的想知道这个。
-
你在你的一个评论中报告你正在看到
connectionDidFinishLoading:
被调用,但是从来没有调用过didReceiveData
。 这意味着(不出意外)没有收到任何数据。 所以,你应该:-
确认
connection:didReceiveResponse:
报告了一个200
的statusCode
; 和 -
确认服务器代码工作正常。 我可以想象得到你描述的行为,如果你的服务器PHP代码有错误(服务器在
php.ini
文件中经常closuresdisplay_errors
会加剧这个错误)。
-
另外,如果与key1
关联的值可能包含任何保留字符(如RFC 3986 第2节中定义的那样),则应该使用CFURLCreateStringByAddingPercentEscapes
来百分比转义string。 从而:
NSString *post = [NSString stringWithFormat:@"key1=%@", [self percentEscapeString:vali]];
其中,根据application/x-www-form-urlencoded
的W3C规范 ,不仅百分比转义,而且还用+
字符replace空格,因此:
- (NSString *)percentEscapeString:(NSString *)string { NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, (CFStringRef)@" ", (CFStringRef)@":/?@!$&'()*+,;=", kCFStringEncodingUTF8)); return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"]; }
使用:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
除非你有一个令人信服的理由不要。
示例(可能不工作):
注意创buildpostData
。
-(void)sendRequest { NSString *vali = @"$uppl!3r$"; NSString *post = [NSString stringWithFormat:@"key1=%@",vali]; NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]; NSString *postLength = [NSString stringWithFormat:@"%lu", [postData length]]; NSLog(@"%@", postLength); NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:@"http://www.ddemo3.enerjinet.com/webservices/ios/suppliers.php"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSLog(@"data: %@", data); }]; }
哦,让生活更轻松,使用ARC
。