Send a POST request Objective-C

最后都变了- 提交于 2019-12-13 08:09:49

问题


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++;
}

回答1:


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


来源:https://stackoverflow.com/questions/33343276/send-a-post-request-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!