Cocoa HTTP POST请求[Mac OS X]

Jul*_*ert 1 html cocoa xmlhttprequest httpresponse http-post

我花了很多时间研究使用Cocoa的POST请求.

我发现一些看起来不错的代码.我改变它就像它符合我的需要.但代码不起作用,我找不到bug,因为我对可可很新.

这是我的代码.

- (IBAction)sendForm:(id)sender
{
    NSLog(@"web request started");
    NSString *post = [NSString stringWithFormat:@"firstName=%@&lastName=%@&eMail=%@&message=%@", firstName.stringValue, lastName.stringValue, eMail.stringValue, message.stringValue];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"myDomain/form.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(theConnection)
    {
        webData = [[NSMutableData data] retain];
        NSLog(@"connection initiated");
    }
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
    NSLog(@"connection received data");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"connection received response");
    NSHTTPURLResponse *ne = (NSHTTPURLResponse *)response;
    if([ne statusCode] == 200)
    {
        NSLog(@"connection state is 200 - all okay");
        NSString *html = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
        [[webView mainFrame] loadHTMLString:html baseURL:[NSURL URLWithString:@"myDomain"]];
    }
}
Run Code Online (Sandbox Code Playgroud)

但我收到的唯一两条NSLog消息是"web request started"和"connection initiated".所以我认为- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data并且- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response没有被召唤.

有人可以帮我解决这个问题吗?

谢谢你的帮助,朱利安

App*_*rew 7

你几乎正确地做到了!

我可以看到这个帖子已经有一年了,但是希望它可以帮助别人并且我自己参考,我在这里发帖.

Apple的文档connection:didReceiveResponse:说: -

在连接收到足够数据以构建其请求的URL响应时发送.

与大多数Apple文档一样,这也令人困惑,也许会产生误导.通常当我们有"足够的数据来构建URL响应"时,意味着我们有完整的响应,这进一步意味着我们也有响应体; 这被普遍认为是回应的一部分.

但是在这种情况下,NSHTTPURLResponse对象没有正文.所以我的猜测是,Apple的意思是 - connection:didReceiveResponse:当我们收到响应标题时会调用它,该标题具有"足够"的数据,让我们知道响应的所有内容,除了实际的数据(正文).

事实上,很多时候你会注意到connection:didReceiveData:调用 connection:didReceiveResponse:调用.

因此,我们可以修改您的代码如下: -

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    NSLog(@"web request started");
    NSString *post = [NSString stringWithFormat:@"firstName=%@&lastName=%@&eMail=%@&message=%@", firstName.stringValue, lastName.stringValue, eMail.stringValue, message.stringValue];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
    NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];

    NSLog(@"Post data: %@", post);

    NSMutableURLRequest *request = [NSMutableURLRequest new];
    [request setURL:[NSURL URLWithString:@"https://example.com/form.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if(theConnection) {
        webData = [NSMutableData data];
        NSLog(@"connection initiated");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [webData appendData:data];
    NSLog(@"connection received data");
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"connection received response");
    NSHTTPURLResponse *ne = (NSHTTPURLResponse *)response;
    if([ne statusCode] == 200) {
        NSLog(@"connection state is 200 - all okay");
    } else {
        NSLog(@"connection state is NOT 200");
    }
}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Conn Err: %@", [error localizedDescription]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Conn finished loading");
    NSString *html = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"OUTPUT:: %@", html);
}
Run Code Online (Sandbox Code Playgroud)