NSHTTPURLResponse中获取Status Code和Error Code 和 Header内容

    
    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    [NSURLConnection sendSynchronousRequest:webView.request returningResponse:&response error:&error];
    
    NSLog(@"%@",webView.request);
    
    int errorCode = (int)[error code];
    NSLog( @"Error Code: %i", errorCode );
    
    int statusCode = (int)[response statusCode];
    NSLog( @"Status Code: %i", statusCode );
    
    if ( [response respondsToSelector:@selector(allHeaderFields)] ) {
        NSDictionary *dictionary = [response allHeaderFields];
        NSLog( @"%@", [dictionary description] );
    }


同样可以根据request创建连接得到response

    NSData *postData = [pair dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString: [[NSString alloc] initWithFormat:@"%@", ACTION_URL]]];
    [request setHTTPMethod: @"POST"];
    [request setValue: HOST forHTTPHeaderField:@"Referer"];
    [request setHTTPBody: postData];
        

pair是参数串

ACTION_URL是提交url

HOST是referer主机名

可以添加其他参数通过 setValue,比如UserAgent等。


你可能感兴趣的:(NSHTTPURLResponse中获取Status Code和Error Code 和 Header内容)