Hai all,
In my iphone project i need to pass the user name and password to a web server,previously i pass data using GET method and used the url with GET format (eg: localhost:8888/login?userName=admin&password=password ) but now i need to sent this as a POST data,
can any one help me to find what wrong in this code below ?
code i tried ..
NSString *post =[NSString stringWithFormat:@"userName=%@&password=%@",userName.text,password.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"https://localhost:443/SSLLogin/login.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(data);
this page will return success or failed using php echo
Well, this exactly is not an answer to your question. But as an alternative, have a look at this code.I use this successfully for sending username and password to server.
You can then implement the following delegate to check the response
Hope this helps.
You are sending the request as NSASCIIStringEncoding but looking for NSUTF8StringEncoding
I'd set
and
however, as Nikolai noted - this would be easier if we knew what the error was :-)
I finally got a way to send data over a secure connection from the iPhone:
to avoid a warning message please add
Courtesy:
To debug HTTP issues, your best bet is to get the Charles HTTP proxy app - it will record all HTTP communication to and from a server, through the simulator (and you can even set it as a proxy for the phone if you need to).
Then, use the program Curl (from Terminal, it is built in) to generate a working post request (you'll have to search online for examples of using CURL). Then you simply compare how a working request from CURL is formatted, to what your application is sending... note that the simulator is automatically re-directed to work through Charles, you have to tell curl the proxy address to use to have things sent through Charles.