iOS HTTP Post 不会运行 url

发布于 2024-12-25 17:12:02 字数 4802 浏览 0 评论 0原文

我正在 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?

I'm performing a HTTP Post in iOS, and using it to run an insert via php on a database. I have a somewhat lengthy form for giving it values to insert, and I pull them all together into a single string, which I include in my method to run the url(right now it's a GET, I've tried it with POST for similar results), and when I test it out, it won't run. I can copy the url it creates and paste that into a browser and run it that way, but it won't run through the iOS application.

I've also tried with ASIHTTPRequest, with no results either way.

Here is my HTTP Post method:

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

and here is the building of the 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;

Can anybody help me figure out why it's not running the url?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有木有妳兜一样 2025-01-01 17:12:02

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

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

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

You need to use this code to get error description:

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

Also, take into account that remote server may read HTTP headers to adopt (or block) your request.

纵性 2025-01-01 17:12:02

这是我发现的问题。我没有正确编码我的 URL。我必须使用 NSUTF8String 编码重新编码我的 URL。 我的同事在这里找到了修复程序。

操作方法如下:

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

Here is the issue I found. I wasn't encoding my URL Correctly. I had to re-encode my URL with NSUTF8String encoding. My co-worker found the fix here.

Here is how you do it:

NSString  *myURL = // some URL
NSString  *fixedURL = [myURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // or any other encoding
NSURL *URL = [NSURL URLWithString :fixedURL];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文