0

这是我第一次通过 HTTP 请求处理服务器通信,为了确定我在做什么,并且有“我的双手之间的事情”,我想打印服务器应该返回给我的 xml 文件. 如果有人可以帮助我,请:

这是我的代码:

(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

if ([stories count] == 0) {
 NSString * l_api_key = @"********************************************";
    NSString * l_secret_key = @"******************************************";

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[@myURLHERE stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
                                                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                        timeoutInterval:60.0];

    [theRequest setHTTPMethod:@"POST"];
    [theRequest setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
    [theRequest setValue:@"application/x-www-form-unrlencoded" forHTTPHeaderField: @"Content-Type"];
    [theRequest addValue:l_api_key forHTTPHeaderField: @"EMApikey"];
    [theRequest addValue:[self hmacsha1:l_api_key secret:l_secret_key] forHTTPHeaderField: @"EMRequestHash"];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    NSLog(@"%@", theConnection);

    if( theConnection )
    {
        NSData *webData = [[NSMutableData data] retain];
        NSLog(@"%@", webData);
        //[self parseXMLFileAtURL:path];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

    [theConnection release];
}

cellSize = CGSizeMake([newsTable bounds].size.width, 60);

}

4

2 回答 2

1

You need to send the POST request and get back the response, see the example below :

//create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];

//post
[theRequest setHTTPBody:postBody];

//get response
NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];  
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
    NSLog(@"Response: %@", result);

            //here you get the response

}
于 2013-11-04T14:58:11.573 回答
1
[NSURLConnection sendAsynchronousRequest:theRequest
                           queue:[NSOperationQueue mainQueue]
               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
    NSLog(@"%@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
}];

它比:

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
于 2013-11-04T15:01:53.447 回答