2012-10-25 3 views
0

서버에서 올바른 출력을 얻지 못했습니다. 나는 매번 다시 얻을 응답은 다음과 같습니다NSURLConnection GET 및 POST가 작동하지 않습니다.

당신은 단지 첫 this 페이지를 통해 페이지 here 대신 갈로 웹 브라우저를 지시 할 때 보통 받았다되어
/prod/bwckgens.p_proc_term_datehas been permanently removed from this server. 

을 사라. 이것은 쿠키가 저장되지 않는다는 결론에 도달하게하지만, 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 

답변

2

동일한 URL 연결이 시작되면 요청 개체를 변경할 수 없습니다. NSURLConnection의 문서를 읽으십시오. 그것은 말합니다 :

로드 할 URL 요청. 요청 개체는 초기화 프로세스의 일부로 딥 복사됩니다. 이 메서드 뒤에 요청을 변경하면 반환은 프로세스를로드하는 데 사용되는 요청에 영향을주지 않습니다.

그래서 다른 URL을 치고 싶다면 새로운 URLRequest와 새로운 URLConnection 객체를 만들어야합니다. 쿠키 저장에 관한 질문과 관련하여 다음 메소드를 사용하여 URLRequest의 캐시 정책을 설정할 수 있습니다.

- (void)setCachePolicy:(NSURLRequestCachePolicy)policy 
+0

문제는 내가 설정하지 않은 리퍼러와 관련된 것이었지만 이것을 수락 된 응답으로 표시합니다. 감사! – kamran619

관련 문제