Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

Send a POST request Objective-C

I need to send a POST request to a website to load more results of the page (There is about 200 results that I want to parse (using TFHpple) but the page is showing just 40 and you need to click "Show More Results" at the button to load more).

I tried to search the web how to send a POST request and I tried but it hasn't succeeded :( Can anyone help me figure it out?

General information:

Remote Address:212.117.156.55:80

Request URL:http://www.d.co.il/DOTNET/ajax/GetMoreResults

Request Method:POST

Status Code:200 OK

File Name: GetMoreResults

If there is any additional information required, just write in and I'll give you it.

Thank you very much!

Am I done this right?

NSURL *url=[NSURL URLWithString:@"http://www.d.co.il/SearchResults/?query=%D7%A9%D7%95%D7%A4%D7%A8%D7%A1%D7%9C&type=tag&location="];
NSData *data=[NSData dataWithContentsOfURL:url];

TFHpple *parser=[TFHpple hppleWithHTMLData:data];

NSString *XpathQueryString=@"//p[contains(@class, "result-contact-address")]";

NSArray *Nodes=[parser searchWithXPathQuery:XpathQueryString];



NSError *error;

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *postUrl = [NSURL URLWithString:@"http://www.d.co.il/DOTNET/ajax/GetMoreResults"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:postUrl
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];

[request addValue:@"GetMoreResults" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"GetMoreResults" forHTTPHeaderField:@"Accept"];

[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"1", @"batchNumber",
                         @"IOS TYPE", @"typemap",
                         nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

}];

[postDataTask resume];

int i=1;
for (TFHppleElement *element in Nodes) {
    NSString *address = element.text;
    NSLog(@"%@,%d",address,i);
    i++;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, you got a down vote (not from me :)) for a reason - this is pretty basic stuff and you get lot of information around with just 2 minutes serious online surfing :)!

I would try to give you a direction here because you want to implement "Show More" feature:

Step 1 : You need to build a service which takes the batchNumber and feeds you data belonging to that batch. So, initially you would send batchNumber = 0 and server returns you first 40 records.

Step 2 : On UI side, return 1 count more than the count of data records you have in hand to show. That additional count is for the last cell Show More.

Step 3 : On tap on Show More, increase your current batchNumber by 1 and make the server call to get the next batch records.

Step 4 : Continue step 2 & 3 util all records are loaded.

Finally, this is how you load server data using a post request:

NSError *error;

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
                     @"IOS TYPE", @"typemap",
                     nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];


NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

}];

[postDataTask resume];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...