NSURLConnection GET和POST无法正常工作

时间:2012-10-25 23:01:23

标签: objective-c ios cocoa-touch nsurlconnection

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

  

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

当您只是将网络浏览器定向到页面here而不是首先浏览this页面时,通常会收到此消息。这让我得出结论,没有保存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

1 个答案:

答案 0 :(得分:2)

启动后,无法使用相同的URL连接更改请求对象。阅读NSURLConnection的文档。它说:

  

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

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

- (void)setCachePolicy:(NSURLRequestCachePolicy)policy