0

我没有从服务器获得正确的输出。我每次得到的回复是:

走了

/prod/bwckgens.p_proc_term_datehas been permanently removed from this server.

当您只是将网络浏览器定向到此处的页面而不是先浏览页面时,通常会收到此信息。这让我得出一个 cookie 没有被保存的结论,但我在文档中读到这一切都由 NSURLConnection 对象处理。我在这里做错了什么吗?

#import "PCFViewController.h"

@interface PCFViewController ()

@end

NSMutableData *mutData;

@implementation PCFViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)queryServer {
    NSURL *url = [NSURL URLWithString:@"https://selfservice.mypurdue.purdue.edu/prod/bwckschd.p_disp_dyn_sched"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:3.0];
    //the reason I perform a GET here is just to get a cookie and communicate like a normal web browser, since directly doing a POST to the proper address isn't working
    [request setHTTPMethod:@"GET"];
    [request setValue:@"text/html; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (connection) {
        mutData = [NSMutableData data];
    }
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[mutData length]);
    NSString *str = [[NSString alloc] initWithData:mutData encoding:NSUTF8StringEncoding];
    //just to see the contents(for debugging)
    NSLog(@"%@", str);

    [self handleConnection:connection];
}

-(void)handleConnection:(NSURLConnection *)connection
{
    //this is the first step
    if ([@"/prod/bwckschd.p_disp_dyn_sched" isEqualToString:[[[connection originalRequest] URL] path]]) {
        //first request
        //POST
        NSURL *url = [NSURL URLWithString:@"https://selfservice.mypurdue.purdue.edu/prod/bwckgens.p_proc_term_date"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:3.0];
        [request setHTTPMethod:@"POST"];
        NSString *args = @"p_calling_proc=bwckschd.p_disp_dyn_sched&p_term=201320";
        NSData *requestBody = [args dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:requestBody];
        connection = [connection initWithRequest:request delegate:self];
        [connection start];
        if (connection) {
            mutData = [NSMutableData data];
        }
        //second step. Here I send the list of classes(COMPUTER SCIENCE) I want to display as well as the term SPRING2013
    }else if([@"/prod/bwckgens.p_proc_term_date" isEqualToString:[[[connection currentRequest] URL] path]]) {
        NSURL *url = [NSURL URLWithString:@"https://selfservice.mypurdue.purdue.edu/prod/bwckschd.p_get_crse_unsec"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed
                                                           timeoutInterval:3.0];
        [request setHTTPMethod:@"POST"];
        NSString *args = @"term_in=201320&sel_subj=dummy&sel_day=dummy&sel_schd=dummy&sel_insm=dummy&sel_camp=dummy&sel_levl=dummy&sel_sess=dummy&sel_instr=dummy&sel_ptrm=dummy&sel_attr=dummy&sel_subj=CS&sel_crse=dummy&sel_title=dummy&sel_schd=%25&sel_from_cred=&sel_to_cred=&sel_camp=%25&sel_ptrm=%25&sel_instr=%25&sel_sess=%25&sel_attr=%25&begin_hh=0&begin_mi=0&begin_ap=a&end_hh=0&end_mi=0&end_ap=a";
        NSData *requestBody = [args dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:requestBody];
        connection = [connection initWithRequest:request delegate:self];
        [connection start];
        if (connection) {
            mutData = [NSMutableData data];
        }
        //the courses should be shown now I have to parse the data
    }else if([@"/prod/bwckschd.p_get_crse_unsec" isEqualToString:[[[connection currentRequest] URL] path]]) {


    }
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@\n", error.description);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [mutData setLength:0];
    NSLog(@"%@\n", response.description);

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mutData appendData:data];
}
@end
4

1 回答 1

2

一旦启动,您就不能使用相同的 URL 连接更改请求对象。阅读 NSURLConnection 的文档。它说:

要加载的 URL 请求。请求对象作为初始化过程的一部分被深度复制。此方法返回后对请求所做的更改不会影响用于加载过程的请求。

所以如果你想点击另一个 URL,你必须创建一个新的 URLRequest 和新的 URLConnection 对象。关于您关于保存 cookie 的问题。您可以使用以下方法设置 URLRequest 的缓存策略

- (void)setCachePolicy:(NSURLRequestCachePolicy)policy
于 2012-10-25T23:19:17.970 回答