开发者

Put JSON encoded array as NSData from NSURLResponse into NSArray

I'm unable to get this to work:

NSString *post = [NSString stringWithFormat:@"userudid=%@"开发者_JS百科, [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


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

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

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜