如何在ios中发送无正文的json POST请求?

如何在ios中发送无正文的json POST请求?,ios,json,Ios,Json,我想发送post请求,但不在ios中发送正文 我试了如下 NSString *url = [NSString stringWithFormat:@"Some URL"]; NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL]; [request a

我想发送post请求,但不在ios中发送正文

我试了如下

 NSString *url = [NSString stringWithFormat:@"Some URL"];
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setHTTPMethod: @"POST"];

[NSURLConnection connectionWithRequest:request delegate:self];
我得到下面的错误

Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo=0x15d62ed0 
NSLocalizedDescription=Could not connect to the server., 
NSUnderlyingError=0x15e522a0 "Could not connect to the server."}


某些URL不是有效的URL!您是否尝试使用其他程序将某些内容发布到URL以确保其正常工作?Hey@Madhu检查我的答案。、、、尝试了第一个,但在第二个示例中出现相同的错误,我们必须发送正文。但我的请求没有正文。您可以尝试我的第一个示例>>>
NSString *appurl=[NSString stringWithFormat:@"Some URL"];
appurl = [appurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:appurl]];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ];
NSString  *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"%@",returnString);
NSString *post =[NSString stringWithFormat:@"email=%@&pw=%@",[_txt_email.text lowercaseString],_txt_password.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@login_new.php",app.Main_url]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);