xcode Iphone sdk,如何将数据发布到网络服务器?有人有例子吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2925088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iphone sdk, How to post data to web server? anybody has example?
提问by user352385
guys, I want to post data to web server, like username and password, on the web server, I used PHP to echo yes or no, I attached all the code. Can anybody help me out, what is wrong with the code. Since it always says incorrect password or username. I tried to test the php code with username and password inside, it is working. So please help me. Thank you.
伙计们,我想将数据发布到网络服务器,例如用户名和密码,在网络服务器上,我使用 PHP 回显是或否,我附上了所有代码。任何人都可以帮助我,代码有什么问题。因为它总是说不正确的密码或用户名。我试图用里面的用户名和密码测试 php 代码,它正在工作。所以请帮助我。谢谢你。
Header file
头文件
@interface kiksyloginViewController : UIViewController {
IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;
IBOutlet UIButton *loginButton;
}
@property (nonatomic, retain) UITextField *usernameField;
@property (nonatomic, retain) UITextField *passwordField;
@property (nonatomic, retain) UIButton *loginButton;
- (IBAction) login: (id) sender;
@end
Implementation file
实现文件
#import "kiksyloginViewController.h"
@implementation kiksyloginViewController
@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;
- (IBAction) login: (id) sender
{
NSString *post =[NSString stringWithFormat:@"username=%@&password=%@",usernameField.text, passwordField.text];
NSString *hostStr = @"http://localhost/userlogin.php";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if([serverOutput isEqualToString:@"Yes"]){
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Congrats" message:@"You are authorized "
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
} else {
UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Username or Password Incorrect"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertsuccess show];
[alertsuccess release];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
PHP file
PHP文件
<?php
//$Container = new Container;
$u = $_GET['username'];
$pw =$_GET['password'];
$check = "select username, password from user where username='$u' and password='$pw'";
function myDbconn()
{
mysql_connect("localhost", "root","") or die();
mysql_select_db("test") or die (mysql_error());
}
myDbconn();
$login = mysql_query($check) or die (mysql_error());
//$run = DB()->select($check);
if (mysql_num_rows($login)==1){
//$row = DB()->fetch($run);
//$row = DB()->fetch($login);
$row = mysql_fetch_assoc($login);
echo 'yes';
exit;
}
else {
echo 'No';
exit;
}
?>
回答by Ed Marty
The first issue I see that's probably the biggest is that there's no "?" anywhere. Specifically, this:
我看到的第一个问题可能是最大的问题是没有“?” 任何地方。具体来说,这:
@"username=%@&password=%@"
should look more like this:
应该看起来更像这样:
@"?username=%@&password=%@"
Because you're not actually posting to the server, you're just using a GET request and URL parameters.
因为您实际上并没有发布到服务器,所以您只是使用了 GET 请求和 URL 参数。
I don't know if you plan on getting a web server and PHP running on your device in the background, but at some point I assume you will change http://localhostto something else, and hopefully you will sanitize your SQL query before you let people navigate to your webpage and type in anything they want in the URL query.
我不知道您是否打算让 Web 服务器和 PHP 在后台运行在您的设备上,但在某些时候我假设您会将http://localhost更改为其他内容,并希望您能在此之前清理您的 SQL 查询您让人们导航到您的网页并在 URL 查询中输入他们想要的任何内容。
回答by MLBDG
The post is old, but anyway if it may help others:
该帖子很旧,但无论如何如果它可以帮助其他人:
Yeah, it had a couple of errors. First, the correct string in file .m should be:
是的,它有几个错误。首先,文件 .m 中的正确字符串应该是:
NSString *post =[NSString stringWithFormat:@"?&username=%@&password=%@",
usernameField.text, passwordField.text];
Also at file .m:
同样在文件 .m:
if([serverOutput isEqualToString:@"yes"]){
instead of "Yes". Be aware of capital letters!
而不是“是”。注意大写字母!
That′s all. The code worked well.
就这样。代码运行良好。