문제

I am trying to send this POST request to the specified website in Objective-C, but know very little about HTML and how to format my request.

The address is: https://selfservice.mypurdue.purdue.edu/prod/bwckgens.p_proc_term_date if you want to see where I am getting this HTML source code from.

    <FORM ACTION="/prod/bwckschd.p_get_crse_unsec" METHOD="POST">
<INPUT TYPE="hidden" NAME="term_in" VALUE="201320">
<INPUT TYPE="hidden" NAME="sel_subj" VALUE="CS">
<INPUT TYPE="hidden" NAME="sel_levl" VALUE="dummy">
<INPUT TYPE="hidden" NAME="sel_schd" VALUE="dummy">
<INPUT TYPE="hidden" NAME="sel_coll" VALUE="dummy">
<INPUT TYPE="hidden" NAME="sel_divs" VALUE="dummy">
<INPUT TYPE="hidden" NAME="sel_dept" VALUE="dummy">
<INPUT TYPE="hidden" NAME="sel_attr" VALUE="dummy">
   </FORM>

I am trying to send a POST request to the address https://selfservice.mypurdue.purdue.edu/prod/bwckctlg.p_display_courses with the the field "term_in" equal to 201320 and the "sel_subj" field equal to "CS", and the rest being dummy. How would I go about doing this?

This is what I have so far:

#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_get_crse_unsec"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:3.0];
    [request setHTTPMethod:@"POST"];
    NSString *args = @"term_in=201320&sel_subj=CS&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";
    NSData *requestBody = [args dataUsingEncoding:NSUTF8StringEncoding];
    [request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:requestBody];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
    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];
    NSLog(@"%@", str);
}

-(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

My console shows me this output:

Succeeded! Received 213 bytes of data
2012-10-25 02:25:38.683 Purdue Course Finder[25844:c07] <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>410 Gone</title>
</head><body>
<h1>Gone</h1>
/prod/bwckschd.p_get_crse_unsechas been permanently removed from this server.</p>
</body></html>

Can someone help me figure out why this is not a valid request? If a value is set to dummy, should I not add it to the string? Is the string in an incorrect format with each value being suffixed with a &? Any suggestions? Can someone get me the URL of this POST request so I can modify my code accoringly?

도움이 되었습니까?

해결책

Try it in this format,

[request setValue:@"201320" forHTTPHeaderField:@"term_in"];
[request setValue:@"CS" forHTTPHeaderField:@"sel_subj"];
[request setValue:@"dummy" forHTTPHeaderField:@"sel_levl"];

다른 팁

You need to supply the fields and their values as the body of your request, and set the content type:

NSString *paramString = [NSString stringWithFormat:@"arg1=%@&arg2=%@", val1, val2];
NSData  *data = [paramString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

Then execute your request.

@try this code

NSURL *url = [NSURL URLWithString:@"https://selfservice.mypurdue.purdue.edu/prod/bwckctlg.p_display_courses"];  

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:3.0];

[request setHTTPMethod:@"POST"];

NSString *args = @"term_in=201320,sel_subj=CS,sel_levl=dummy,sel_schd=dummy,sel_coll=dummy"sel_divs=dummy,sel_dept=dummy,sel_attr=dummy";

NSData *requestBody = [args dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:requestBody];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

handle response or error with NSURLConnectionDelegate Methods

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top