我正在努力弄清楚如何在我正在通過Objective-C與之交互的網站上連接到此塊HTML
。該網頁有一個可以輸入的文本區域,下面有一個提交按鈕。該HTML
看起來是這樣的:如何使用NSURLConnection發佈到「textarea」?
<fieldset>
<textarea id="postbody" cols="40" rows="5" name="postbody">
This has now been taken. Thank you to everyone who has shown an interest.
</textarea>
<br>
<input type="submit" value="Send TAKEN message">
</fieldset>
無論如何,我想與本網站使用NSURLConnection
接口:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:modifiedURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSString *convertedString=[NSString stringWithFormat:@"postbody=%@", @"This item has been received"];
[request setHTTPBody:[NSData dataWithBytes:[convertedString UTF8String] length:strlen([convertedString UTF8String])]];//this attaches the username and password key we created with convertedString to the request we just initialized
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];//we're sending information TO the site
self.safetyRequest = request;//this is to handle the possibility that the user hits back and chooses the same menu item more than once.
[self setReceivedData:[NSMutableData dataWithCapacity: 0]];//make sure we re-initialize our data container
[self setTheConnection:[[NSURLConnection alloc] initWithRequest:request delegate:self]];//this fires off the URL request to the web!! This is an asynchronous request, so we need to implement the delegates for NSURLConnection.
然而,這不適用於某些原因。該網站沒有迴應,並執行發佈回覆並提交它的所需操作。我究竟做錯了什麼?
你能否澄清 「網站沒有反應」?你有沒有看過'connection:didReceiveResponse:'中收到的迴應?什麼是'statusCode'?你在'connection:didFailWithError:'中處理了錯誤嗎?那裏有什麼報道?即使響應格式錯誤,您至少也會期待'didReceiveResponse'或'didFailWithError'。 – Rob
我發現我做錯了什麼。我實際上發佈到一個不接受POST的URL。當您點擊提交按鈕時,它實際上會將該信息發佈到其他網址。一旦我發現,一切正常。感謝大家的迴應! – Dave