我有一个特定的URL“http://dev.mycompany.com”,我需要向其发送一些数据(JSON格式)并获得响应。
我在SO上的大量文档和相关问题中找不到自己的方式。我已经设法通过NSURLConnection获取数据(不发送任何数据),并且效果很好,到目前为止,我所拥有的代码与https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html上的代码几乎相同,因此我看不到发布任何代码的意义。
我无法在URL后面附加一些字符串,因为我需要发送的数据是JSON数据。如果听起来像菜鸟,很抱歉,但是我在Obj-C和服务器通信方面的经验很少。
最佳答案
您应该将它们作为参数发送到帖子NSUrlRequest
如下:
NSURL *aUrl = [NSURL URLWithString:@"http://dev.mycompany.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"yourVarialbes=yourvalues";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
用于发送JSON
请阅读
How to send json data in the Http request using NSURLRequest
关于ios - 将JSON数据发送到URL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10864497/