Cannot Post to site using Xcode.
Ok everyone I could really use a little help here. I am very new to coding in xcode(also this is the first time I have ever asked for help on a site). I need to make a call to an ASP page with variables past to it. I have tried the below code but nothing seems to be working.
When I type this into a URL it works fine.
http://devsite.com/login.asp?longitude=-1737630384&开发者_如何学Golatitude=-567677783&device=4FFB39E4-28A8-5E26-90C5-6E16105E08C8&username=Me&service=testsubject1&comments=Helloworld
However I have tried variations of the below code and nothing seems to work
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];
You seem to be trying to force something into a POST method that is designed to be using URL parameters via an HTTP GET method. When you type in the URL and it works, I assume you mean in a browser URL bar. That means it will be doing a GET, not a POST.
Instead of trying to assemble your URI parameters and stick them in the POST message body, you should add them to your URL and do a simple GET request using NSURLRequest. Try something like this:
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];
精彩评论