发送POST请求Objective-C [英] Send a POST request Objective-C

查看:107
本文介绍了发送POST请求Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要向网站发送POST请求以加载页面的更多结果(我想解析大约200个结果(使用TFHpple),但是页面仅显示40个,您需要点击显示更多"结果"按钮以加载更多).

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

我试图在网络上搜索如何发送POST请求,但尝试了但没有成功:(谁能帮我解决这个问题?

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?

一般信息:

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.

非常感谢!

我这样做正确吗?

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

推荐答案

好吧,您有一个不赞成的投票(不是我的:))-这是非常基本的东西,您仅需2就可以得到很多信息分钟严重的在线冲浪:)!

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:

第1步::您需要构建一个使用batchNumber并向您提供属于该批次的数据的服务.因此,最初,您将发送batchNumber = 0,而服务器将向您返回前40条记录.

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.

第2步::在用户界面方面,返回的计数要比您要显示的数据记录的计数多1.该额外计数用于最后一个单元格显示更多.

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.

第3步::点击 Show More (显示更多),将当前的batchNumber增加1,并进行服务器调用以获取下一个批处理记录.

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

第4步::继续第2步& 3 util加载了所有记录.

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

这篇关于发送POST请求Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆