iphone将数据发布到网址

时间:2011-03-22 18:01:35

标签: iphone objective-c cocoa ios4

这是我需要发布http://www.mysite.com/save?details= {“name”}

的字符串

我尝试以下代码;

NSString * post = [NSString stringWithFormat:@“details = {\”name \“}”];

NSString *hostStr = @"http://www.mysite.com/save?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL =  [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr]];    
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];

但serverOutput总是= null,除非我只是{name}。但我需要那里的qutoes :(

2 个答案:

答案 0 :(得分:0)

您应该将所有内容转换为有效的HTTP符号。

e.g:

hostStr = [hostStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

...我的猜测是您的网络服务器拒绝无效网址。

答案 1 :(得分:0)

错误..

NSString * post = [NSString stringWithFormat:@“details = {\”name \“}”];

<强>正确:

NSString * post = [NSString stringWithString:@“details = {\”name \“}”];