好的每个人我真的可以在这里使用一点帮助。我是xcode编码的新手(这也是我第一次在网站上寻求帮助)。我需要调用一个包含变量的ASP页面。我尝试了下面的代码,但似乎没有任何工作。
当我将其输入到网址时,它可以正常工作。
但是我尝试了以下代码的变体,似乎没有任何效果
NSString *post = @"?longitude=";
post = [post stringByAppendingString:longt];
post = [post stringByAppendingString:@"&latitude="];
post = [post stringByAppendingString:lat];
post = [post stringByAppendingString:@"&device="];
post = [post stringByAppendingString:deviceUDID];
post = [post stringByAppendingString:@"&username="];
post = [post stringByAppendingString:username.text];
post = [post stringByAppendingString:@"&service="];
post = [post stringByAppendingString:subject.text];
post = [post stringByAppendingString:@"&comments="];
post = [post stringByAppendingString:comments.text];
//post = [[NSString alloc] initWithFormat:@"&longitude=%@", longt];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSString *lsUrl = @"http://devi.asp";
lsUrl = [lsUrl stringByAppendingString:post];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:lsUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
答案 0 :(得分:0)
您似乎试图将某些内容强制转换为POST方法,该方法旨在通过HTTP GET方法使用URL参数。当您输入URL并且它有效时,我认为您的意思是在浏览器URL栏中。这意味着它将进行GET,而不是POST。
您应该将它们添加到您的URL并使用NSURLRequest执行简单的GET请求,而不是尝试组合您的URI参数并将它们粘贴到POST邮件正文中。尝试这样的事情:
NSString *lsUrl = [NSString stringWithFormat:@"http://devi.asp%@", post];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:lsUrl];
NSURLResponse *response;
NSError *error;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];