2011-11-21 45 views
0

我正在創建一個可可程序,它只是將鍵入到文本字段的數據發送到HTTP表單。Cocoa HTTP POST到命名文本字段

我有所有的各種連接細節計算出來,我似乎無法得到發佈的字符串來定位文本字段。這是我有以下形式:

<html> 
<body> 

<form action='test.php' method='post'> 
    Name: <input type='text' name='nameOfPerson'/> 
    Age: <input type='text' name='ageOfPerson'/> 
    <input type='submit'/> 
</form> 

</body> 
</html> 

和可可......

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
            initWithURL:[NSURL URLWithString:@"http://localhost:8888/CocoaFormPractice/form.html"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"text/html" forHTTPHeaderField:@"Content-type"]; 

    NSString *xmlString = @"nameOfPerson:test&ageOfPerson:123"; 

    [request setValue:[NSString stringWithFormat:@"%d", [xmlString length]] forHTTPHeaderField:@"Content-length"]; 
    [request setHTTPBody:[xmlString dataUsingEncoding:NSUTF8StringEncoding]]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    [request release]; 

我知道,我使用的的xmlString值是假的,甚至不是XML。

回答

0

你大多是在正確的軌道:

替換冒號「:」用符號「=」你的xmlString。

NSString *xmlString = @"nameOfPerson=test&ageOfPerson=123"; 

另外,它不是XML,因此您可以將其稱爲「queryString」。

+0

太好了,非常感謝Sanjay - 我仍然沒有收到表單的值,但我懷疑它一定是我在代碼中發現的其他東西,而不是你的建議。 – patrickn

+0

您使用什麼將請求發送到服務器?看看下面的內容:[鏈接](http://stackoverflow.com/questions/5537297/ios-how-to-perform-a-http-post-request/5537501#5537501) –