Objective c 包含正文内容的HTTP Post请求失败
我正在尝试从objective-c应用程序向服务器发送HTTP post请求。我尝试了许多不同的方法,但我无法让请求处理请求正文中的内容。使用url上的参数,它可以正常工作,但我需要发送一个带有保留字符的字符串 这是我的班级负责的请求:Objective c 包含正文内容的HTTP Post请求失败,objective-c,cocoa,http,Objective C,Cocoa,Http,我正在尝试从objective-c应用程序向服务器发送HTTP post请求。我尝试了许多不同的方法,但我无法让请求处理请求正文中的内容。使用url上的参数,它可以正常工作,但我需要发送一个带有保留字符的字符串 这是我的班级负责的请求: #import "CLPUtilRequest.h" static NSString *baseURL = @"http://localhost:8080/cl-mobile"; @implementation CLPUtilRequest //Parame
#import "CLPUtilRequest.h"
static NSString *baseURL = @"http://localhost:8080/cl-mobile";
@implementation CLPUtilRequest
//Parameteres on the URL (working!!)
+ (NSData *) makeHttpRequest:(NSString *)url {
//Set info for webservice
NSString *urlString = baseURL;
urlString = [urlString stringByAppendingString:url];
NSURL *resquestUrl = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:resquestUrl];
[request setHTTPMethod: @"POST"];
[request setValue:@"application/http" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//Call the webservice
NSError *errorReturned = nil;
NSHTTPURLResponse *theResponse =[[NSHTTPURLResponse alloc]init];
return [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
}
//Parameteres on the bodt (not working!!)
+ (NSData *) makeHttpRequestContentOnBody:(NSString *)url withContent:(NSString *)content {
NSData *postData = [content dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *errorReturned = nil;
NSHTTPURLResponse *theResponse =[[NSHTTPURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
return data;
}
@end
这就是我提出请求的地方
NSString *contentString = @"chaveUsuarioEmpresa=";
contentString = [contentString stringByAppendingString:appDelegate.clinica.chave];
contentString = [contentString stringByAppendingString:@"&chaveAgendamento="];
contentString = [contentString stringByAppendingString:appDelegate.agendamento.chave];
contentString = [contentString stringByAppendingString:@"&anamnese="];
contentString = [contentString stringByAppendingString:self.texto.text];
NSData *dataResponse = [CLPUtilRequest makeHttpRequestContentOnBody:@"/agendamento/anamnese/save" withContent:contentString];
调用请求后,my errorReturned为我提供以下调试:nsurError*域:@nsurErrorDomain-代码:-1002
我尝试按照此链接中的描述进行操作,但没有成功:
提前谢谢 根据-1002,是不受支持的url的代码。实际上,您正在将其发送到/agendamento/anamnese/save,因为您不像在另一个调用中那样预先添加baseURL
+ (NSData *) makeHttpRequest:(NSString *)url {
//Set info for webservice
NSString *urlString = baseURL;
urlString = [urlString stringByAppendingString:url];
NSURL *resquestUrl = [NSURL URLWithString:urlString];
在makeHttpRequestContentOnBody:withContent:中,使用未更改的url字符串/agendamento/anamnese/save
试一试
非常感谢,伙计,这真是我的一个愚蠢的错误:现在它工作得很好!
+ (NSData *) makeHttpRequestContentOnBody:(NSString *)url withContent:(NSString *)content
{
NSData *postData = [content dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *urlString = baseURL;
urlString = [urlString stringByAppendingString:url];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *errorReturned = nil;
NSHTTPURLResponse *theResponse =[[NSHTTPURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
return data;
}