gpt4 book ai didi

iOS HTTP Post 不会运行 url

转载 作者:行者123 更新时间:2023-11-29 11:17:31 26 4
gpt4 key购买 nike

我在 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;

谁能帮我弄清楚为什么它不运行 url?

最佳答案

您需要使用此代码来获取错误描述:

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

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

关于iOS HTTP Post 不会运行 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8779994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com