将JSON编码的数组作为NSData从NSURLResponse到NSArray。

时间:2022-10-24 14:06:49

I'm unable to get this to work:

我无法让它起作用:

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);

is the POST formed correctly? Because i can get it to work from a manual html form posting to the same php file, but i get nothing back when doing it from iOS

这篇文章是正确的吗?因为我可以将它从一个手动html表单发布到相同的php文件中,但是在iOS中我什么也没有得到

1 个解决方案

#1


1  

You might have an easier time doing HTTP POST's with ASIHTTPRequest.

使用ASIHTTPRequest处理HTTP POST可能会更方便。

Form-Posting with ASIHTTPRequest is explained in the section titled "Sending a form POST with ASIFormDataRequest" here: http://allseeing-i.com/ASIHTTPRequest/How-to-use

ASIHTTPRequest的表单发布在“用ASIFormDataRequest发送表单帖子”一节中得到了解释:http://allseeing- i.com/asihttprequest/how to-use

Otherwise, have a look at the NSURLResponse, check the response code and see if iOS thinks the POST was successful.

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

Other thing you can use to check is a tool like wireshark to compare the network traffic from your web-based form POST to the traffic from your iOS POST. It looks like you're doing it right, but the best way to be sure is something like wireshark.

你还可以使用wireshark这样的工具来比较基于web的表单和iOS的流。看起来你做得很对,但是最好的方法是像wireshark这样的公司。

#1


1  

You might have an easier time doing HTTP POST's with ASIHTTPRequest.

使用ASIHTTPRequest处理HTTP POST可能会更方便。

Form-Posting with ASIHTTPRequest is explained in the section titled "Sending a form POST with ASIFormDataRequest" here: http://allseeing-i.com/ASIHTTPRequest/How-to-use

ASIHTTPRequest的表单发布在“用ASIFormDataRequest发送表单帖子”一节中得到了解释:http://allseeing- i.com/asihttprequest/how to-use

Otherwise, have a look at the NSURLResponse, check the response code and see if iOS thinks the POST was successful.

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

Other thing you can use to check is a tool like wireshark to compare the network traffic from your web-based form POST to the traffic from your iOS POST. It looks like you're doing it right, but the best way to be sure is something like wireshark.

你还可以使用wireshark这样的工具来比较基于web的表单和iOS的流。看起来你做得很对,但是最好的方法是像wireshark这样的公司。