2010-11-25 3 views
1

iphoneからウェブサーバーにデータを渡すためにこのhtmlフォームを持っています。 しかし、私はこのフォーム/データを変更可能なリクエストに構築する方法を悩まされています。あなたはplsできますか?アドバイスを下さい。URLリクエストフォーム

HTMLフォーム:

<html> 
<form method="post" action="https://mysite.com"> 
<input type="hidden" name="action" value="sale"> 
<input type="hidden" name="acctid" value="TEST123"> 
<input type="hidden" name="amount" value="1.00"> 
<input type="hidden" name="name" value="Joe Customer"> 
<input type="submit"> 
</form> 
</html> 

私は特定のキー(。例:アクション、acctid、量、名)へのURLリクエストで「値」を割り当てる方法がわかりません?

これは私のコードです:

NSString *urlString = @"https://mysite.com"; 
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 

NSString *post = [[NSString alloc] initWithFormat:@"%@%@&%@%@&%@%@&%@%@", 
    action, sale, 
    acctid, TEST123, 
    amount, 1.00, 
             name, Joe Customer]; // ???? 

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];  
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

[urlRequest setHTTPMethod:@"POST"]; 
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; // multipart/form-data 
[urlRequest setHTTPBody:postData]; 

答えて

3

は、あなたが不足しているが、いくつかは、あなたのフォーマット文字列で等号、そしてあなたが@""であなたの文字列の引数をラップする必要があり、右側についてルックス:

NSString *post = [[NSString alloc] initWithFormat:@"%@=%@&%@=%@&%@=%@&%@=%@", 
    @"action", @"sale", 
    @"acctid", @"TEST123", 
    @"amount", @"1.00", 
    @"name", @"Joe Customer"]; 

さらに拡張可能なソリューションの場合は、キー/値のペアを辞書に保存してから、次のようにします。

(クマ心の中であなたがキーと値をURLエンコードが必要な場合があります。)

+0

はそれを得た。..おかげでサイモンを.. –

関連する問題