iOS HTTP Post不会运行url

时间:2012-01-08 18:20:30

标签: ios http-post

我正在iOS中执行HTTP Post,并使用它在数据库上通过php运行插入。我有一个有点冗长的形式给它插入值,我将它们全部拉到一个单独的字符串中,我在我的方法中包含运行url(现在它是一个GET,我用POST尝试过类似的结果),当我测试它时,它将无法运行。我可以复制它创建的url并将其粘贴到浏览器中并以这种方式运行,但它不会通过iOS应用程序运行。

我也尝试过使用ASIHTTPRequest,但没有任何结果。

这是我的HTTP Post方法:

NSString *content = @"";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:inserturl1]];
NSLog(inserturl1);
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]];


// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSArray *results = [returnString componentsSeparatedByString:@"<br />"];
NSLog([results objectAtIndex:0]);

这里是URL的构建:

NSString *fn = [firstname text]; // First Name, fn from insert url
    NSString *ln = [lastname text]; // Last Name, ln from url
    NSString *age = [ageLabel text];
    NSString *als = [address text]; // Address last seen, als from insert url
    NSString *cls = [city text]; // City last seen, cls from insert url
    NSString *sls = [stateLabel text]; // State last seen, sls from insert url
    NSString *jls = [jurisdiction text]; // Jursidiction last seen, jls from insert url
    NSString *countystring = [county text];
    NSString *mdate = [dateLabel text]; // Missing Date, mdate from insert url
    NSString *mtime = [timeLabel text]; // Missing Time, mtime from insert url
    NSString *dob = [dobLabel text]; // Date of Birth, dob from insert url
    dob = [dob stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
    NSString *race = [ethnicityLabel text];
    NSString *heightstringft = [heightft text];
    NSString *heightstringin = [heightin text];
    NSString *gender = [genderLabel text];
    NSString *weightstring = [weight text];
    NSString *hair = [hairColorLabel text];
    NSString *eye = [eyeColorLabel text];
    NSString *cloths = [clothingdescription text];
    NSString *baseurl = @"http://my.domain.tld/php/newCase1Insert.php?cn=";
    NSString *buildurl = [baseurl stringByAppendingFormat:casenumber];
    buildurl = [buildurl stringByAppendingString:@"&fn="];
    buildurl = [buildurl stringByAppendingFormat:fn];
    buildurl = [buildurl stringByAppendingString:@"&ln="];
    buildurl = [buildurl stringByAppendingFormat:ln];
    buildurl = [buildurl stringByAppendingString:@"&age="];
    buildurl = [buildurl stringByAppendingFormat:age];
    buildurl = [buildurl stringByAppendingString:@"&als="];
    buildurl = [buildurl stringByAppendingFormat:als];
    buildurl = [buildurl stringByAppendingString:@"&cls="];
    buildurl = [buildurl stringByAppendingFormat:cls];
    buildurl = [buildurl stringByAppendingString:@"&sls="];
    buildurl = [buildurl stringByAppendingFormat:sls];
    buildurl = [buildurl stringByAppendingString:@"&jls="];
    buildurl = [buildurl stringByAppendingFormat:jls];
    buildurl = [buildurl stringByAppendingString:@"&mdate="];
    buildurl = [buildurl stringByAppendingFormat:mdate];
    buildurl = [buildurl stringByAppendingString:@"&mtime="];
    buildurl = [buildurl stringByAppendingFormat:mtime];
    buildurl = [buildurl stringByAppendingString:@"&dob="];
    buildurl = [buildurl stringByAppendingFormat:dob];
    buildurl = [buildurl stringByAppendingString:@"&race="];
    buildurl = [buildurl stringByAppendingFormat:race];
    buildurl = [buildurl stringByAppendingString:@"&height="];
    buildurl = [buildurl stringByAppendingFormat:heightstringft];
    buildurl = [buildurl stringByAppendingFormat:heightstringin];
    buildurl = [buildurl stringByAppendingString:@"&gender="];
    buildurl = [buildurl stringByAppendingFormat:gender];
    buildurl = [buildurl stringByAppendingString:@"&weight="];
    buildurl = [buildurl stringByAppendingFormat:weightstring];
    buildurl = [buildurl stringByAppendingString:@"&hair="];
    buildurl = [buildurl stringByAppendingFormat:hair];
    buildurl = [buildurl stringByAppendingString:@"&eye="];
    buildurl = [buildurl stringByAppendingFormat:eye];
    buildurl = [buildurl stringByAppendingString:@"&cloths="];
    buildurl = [buildurl stringByAppendingFormat:cloths];
    buildurl = [buildurl stringByAppendingString:@"&county="];
    buildurl = [buildurl stringByAppendingFormat:countystring];
    buildurl = [buildurl stringByReplacingOccurrencesOfString:@" " withString:@"%%20"];
    NSLog(buildurl);
    inserturl1 = buildurl;

有人可以帮我弄清楚为什么它没有运行网址吗?

2 个答案:

答案 0 :(得分:2)

您需要使用此代码来获取错误说明:

NSError *error = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSLog(@"Error: %@", [error localizedDescription]);

另外,请考虑远程服务器可能会读取HTTP标头以采用(或阻止)您的请求。

答案 1 :(得分:0)

这是我发现的问题。我没有正确编码我的URL。我不得不用NSUTF8String编码重新编码我的URL。 My co-worker found the fix here.

以下是您的操作方法:

NSString  *myURL = // some URL
NSString  *fixedURL = [myURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // or any other encoding
NSURL *URL = [NSURL URLWithString :fixedURL];