2011-11-07 9 views
-1

GETを使用してiPhoneアプリケーションからオンラインフォームにデータ(ユーザー名とパスワード)を送信しようとしています。iPhoneアプリケーションからPOSTを使用してWebフォームにデータを送信する

これは私のソースコードですが、問題は自分のデータベースにデータが格納されないということです。

NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",enterUserName.text,enterPass.text]; 
NSURL *url=[NSURL URLWithString:@"http://planetimpressions.com/contacts/imagecomm_register.php"]; 

NSLog(post); 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setHTTPBody:postData]; 

/* when we user https, we need to allow any HTTPS cerificates, so add the one line code,to tell teh NSURLRequest to accept any https certificate, i'm not sure about the security aspects 
*/ 

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 

NSError *error; 
NSURLResponse *response; 
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",data); 

ウェブフォームのソースコードは以下のとおりです。

if(isset($_GET['submit'])){ 
$email = $_GET['email']; 
$password = $_GET['password']; 
$conn = mysql_connect("mysql", "*****", "*****"); 
if(!$conn) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db("imagecomm_users"); 
$sql = "INSERT INTO registration(number, email, password) VALUES (null, 
'$email', '$password')"; 
if (!mysql_query($sql,$conn)) 
    { 
    die('Error: ' . mysql_error()); 
    } 
} 
?> 

私はスクリプトをテストしましたが、それは問題なく動作します。私はユーザー名とパスワードをオンラインフォームを使ってデータベースに保存することができます。私が抱えている唯一の問題は、アプリケーションからオンラインフォームに接続することです。

コンソールにエラーメッセージが表示されない、または予期しないことが起こっています。ただ、何も起こりません。私はどんな助けにも感謝します。ありがとう

+0

間違いはありますか?最終的なログには予期しないことがありますか?ネットワーク上のパケットトレースは、あなたが期待するものを示していますか?受信した内容を確認するためにサーバーをデバッグしましたか?あなたはデータベースが正しく接続されていることを知っていますか?このコードとデータベースとの間には大きなギャップがあり、このコードスニペットはほんの少ししか扱っていません。このような質問を投稿する前に、より多くの研究を行い、わからない場合はその研究をどうやって行うのかを尋ねてください。 –

+0

は、あなたが提案した変更を反映するために質問を編集しました –

答えて

0

問題を解決するためにUIWebViewを使用しました。

コードのUIWebViewを呼び出すは - ヘッダファイルに

実装ファイルで

UIWebView *myWebView 
@property(nonatomic, retain) IBOutlet UIWebView ... 

を宣言 -

[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URLTOCALL]]]; 

と二つの方法 -

- (void)webViewDidFinishLoad:(UIWebView *)webView 

そして

- (void)webViewDidStartLoad:(UIWebView *)webView{ 
1

サーバーで要求/応答のASIHTTP要求を使用します。 ASIHTTPファイルはhttp://github.com/pokeb/asi-http-request/tarball/masterからダウンロードしてください。 内部プロジェクトを統合し、ヘッダーに追加するだけでなく、.hファイルでデリゲートを定義します。 このコードをファイル内で使用できます。

- (void)requestAuth { 
    NSString* urlString = [NSString stringWithFormat:@" http://planetimpressions.com/contacts/imagecomm_register.php 
    "]; 
    NSURL *url = [NSURL URLWithString:self.urlString]; 
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:url]; 

    if (!request) { 
     NSLog(@"Request prepared"); 
     return nil; 
    } 

    [request setPostValue: enterUserName.text forKey:@”email”]; 

    [request setPostValue: enterPass.text forKey:@"password"]; 
    [request setTimeOutSeconds:30]; 

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 
    [request setShouldContinueWhenAppEntersBackground:YES]; 
    #endif 

    [request setDelegate:self]; 
    [request setDidFailSelector:@selector(ASIHTTPUploadFailed:)]; 
    [request setDidFinishSelector:@selector(ASIHTTPUploadFinished:)]; 
    [request startAsynchronous]; 
} 

- (void)ASIHTTPUploadFinished:(ASIHTTPRequest *)theRequest { 
    //Parse coming response 
    NSLog(@"%@",[theRequest responseString]); 
} 

- (void)ASIHTTPUploadFailed:(ASIHTTPRequest *)theRequest { 
    //Parse response if request become failure 
    NSLog(@"%@",[theRequest responseString]); 
} 
+0

ASIHTTPRequestは廃止されました。あなたはそれを使用しないことをお勧めします:http://allseeing-i.com/%5Brequest_release%5D –

関連する問題