如何使用NSURLConnection发布到“textarea”?

时间:2014-01-22 02:56:37

标签: html ios nsurlconnection nsurlrequest

我正在努力弄清楚如何在我通过Objective-C与之交互的网站上与HTML的这个块进行交互。该网页有一个您可以输入的文本区域,它下面有一个提交按钮。 HTML看起来像这样:

<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.

然而,由于某些原因,这不起作用。该站点不响应并执行发布响应和提交响应所需的操作。我做错了什么?

2 个答案:

答案 0 :(得分:0)

正如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)@":/?@!$&'()*+,;=", kCFStringEncodingUTF8)); return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"]; } 的方法:

{{1}}

答案 1 :(得分: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;
?>