gpt4 book ai didi

iphone - POST 请求到 Objective-C 网站

转载 作者:行者123 更新时间:2023-12-03 20:27:48 24 4
gpt4 key购买 nike

我正在尝试使用 Objective-C 将此 POST 请求发送到指定网站,但对 HTML 以及如何格式化我的请求知之甚少。

地址是:https://selfservice.mypurdue.purdue.edu/prod/bwckgens.p_proc_term_date如果您想查看我从哪里获取此 HTML 源代码。

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

我正在尝试向地址 https://selfservice.mypurdue.purdue.edu/prod/bwckctlg.p_display_courses 发送 POST 请求其中“term_in”字段等于201320,“sel_subj”字段等于“CS”,其余为虚拟。我该怎么做呢?

这是我到目前为止所拥有的:

#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

我的控制台向我显示以下输出:

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>

有人可以帮我弄清楚为什么这不是一个有效的请求吗?如果一个值设置为虚拟,我不应该将其添加到字符串中吗?字符串格式是否不正确,每个值都带有 & 后缀?有什么建议么?有人可以给我这个 POST 请求的 URL,以便我可以相应地修改我的代码吗?

最佳答案

您需要提供字段及其值作为请求的正文,并设置内容类型:

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"];

然后执行您的请求。

关于iphone - POST 请求到 Objective-C 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com