2014-01-22 139 views
0

我正在努力弄清楚如何在我正在通過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. 

然而,這不適用於某些原因。該網站沒有迴應,並執行發佈回覆並提交它的所需操作。我究竟做錯了什麼?

+0

你能否澄清 「網站沒有反應」?你有沒有看過'connection:didReceiveResponse:'中收到的迴應?什麼是'statusCode'?你在'connection:didFailWithError:'中處理了錯誤嗎?那裏有什麼報道?即使響應格式錯誤,您至少也會期待'didReceiveResponse'或'didFailWithError'。 – Rob

+0

我發現我做錯了什麼。我實際上發佈到一個不接受POST的URL。當您點擊提交按鈕時,它實際上會將該信息發佈到其他網址。一旦我發現,一切正常。感謝大家的迴應! – Dave

回答

0

正如W3C spec概述了application/x-www-form-urlencoded,你應該%的轉義您發佈的數據,你可以用CFURLCreateStringByAddingPercentEscapes做,如:

NSString *convertedString = [NSString stringWithFormat:@"postbody=%@", [self percentEscapeString:@"This item has been received"]]; 
[request setHTTPBody:[convertedString dataUsingEncoding:NSUTF8StringEncoding]]; 

如果你有一個調用CFURLCreateStringByAddingPercentEscapes的方法:

- (NSString *)percentEscapeString:(NSString *)string 
{ 
    NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, 
                       (CFStringRef)string, 
                       (CFStringRef)@" ", 
                       (CFStringRef)@":/[email protected]!$&'()*+,;=", 
                       kCFStringEncodingUTF8)); 
    return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 
} 
+0

可能不用指出,但使用像[AFNetworking](https://github.com/AFNetworking/AFNetworking)這樣的框架可以幫助您擺脫構建格式良好的'x-www-form-urlencoded'請求的雜草。值得考慮。 – Rob

0

處理與NSURLConnection的委託方法幫助你的結果數據

NSString *post = [NSString stringWithFormat:@"postBody=%@",@"Raja"]; 
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/promos/index.php"]]; 


    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:postData]; 

    NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self]; 

    if(theConnection){ 
     // indicator.hidden = NO; 
     mutableData = [[NSMutableData alloc]init]; 
    } 

你的PHP代碼

<?php 
    $result = $_POST['postBody']; 


    echo $result; 
?>