带有正文内容的HTTP Post请求失败

时间:2014-09-29 18:48:14

标签: objective-c cocoa http

我正在尝试从我的objective-c应用程序向我的服务器发送HTTP post请求。我尝试了很多不同的东西,但我无法得到请求处理请求正文的内容。使用url上的参数它可以正常工作,但我需要发送一个带有保留字符的字符串。

这是我的班级负责请求:

#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];

调用请求后,我的errorReturned给了我以下(调试):NSURLError * domain:@“NSURLErrorDomain” - 代码:-1002

我尝试按照此链接中的说明进行操作(但没有成功):Sending an HTTP POST request on iOS

提前致谢!

1 个答案:

答案 0 :(得分:0)

根据Apple's Error Codes -1002是不受支持的网址的代码。实际上你将它发送到/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;
}