【问题标题】:Put JSON encoded array as NSData from NSURLResponse into NSArray将 JSON 编码的数组作为 NSData 从 NSURLResponse 放入 NSArray
【发布时间】:2011-04-20 07:04:58
【问题描述】:

我无法让它工作:

NSString *post = [NSString stringWithFormat:@"userudid=%@", [udid stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSLog(@"%@",post);
NSData *postData = [NSData dataWithBytes:[post UTF8String] length:[post length]]; 
//[udid dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSLog(@"%@",postData);
//NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myserver.com/myapp/readtags2.php"]];
[request setURL:url];
[request setHTTPMethod:@"POST"];
//[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request 
    returningResponse:&response error:&error];

NSString *content = [NSString stringWithUTF8String:[urlData bytes]];
NSLog(@"responseData: %@", content);

POST 格式是否正确?因为我可以通过手动 html 表单发布到同一个 php 文件来使其工作,但是从 iOS 执行此操作时我一无所获

【问题讨论】:

    标签: objective-c json ios


    【解决方案1】:

    使用 ASIHTTPRequest 进行 HTTP POST 可能会更轻松。

    使用 ASIHTTPRequest 进行表单发布在此处标题为“使用 ASIFormDataRequest 发送表单 POST”的部分中进行了说明:http://allseeing-i.com/ASIHTTPRequest/How-to-use

    否则,看看 NSURLResponse,检查响应代码,看看 iOS 是否认为 POST 成功。

    您可以用来检查的其他东西是像wireshark 这样的工具,用于比较来自基于Web 的表单POST 的网络流量与来自iOS POST 的流量。 看起来你做对了,但最好的方法是使用wireshark。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多