objective c post get request with body - objective-c

I have the server and encrypt:93mrLIMApU1lNM619WzZje4S9EeI4L2L.
I want to connect and get json from it. I tried:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"http://mydomain.co.il/clubs"]];
[request setHTTPMethod:#"GET"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"requestReply: %#", requestReply);
}] resume];
But I get: html source with "Invalid Encryption key".
Somebody said me that without body I'll not get the result. What should I do? And what about encrypt:93mrLIMApU1lNM619WzZje4S9EeI4L2L ?

Your encoding of the data might be wrong, try changing it to utf-8 encoding like this:
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Related

Https Post in objective c

I have the below code for http post request,
FIRDatabaseReference *agreementCreateReference = [[[FIRDatabase database] referenceWithPath:#"/agreements/"] childByAutoId];
NSLog(#"autoId %#",agreementCreateReference.key);
NSLog(#"autoId %#",_propertyId);
NSString *post = [NSString stringWithFormat:#"agreementId=%#&listingId=%#",agreementCreateReference.key,_propertyId];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"https://krib-api-onbit.herokuapp.com/api/agreements"]];
[request setHTTPMethod:#"POST"];
[request setValue:idToken forHTTPHeaderField:#"X-FIREBASE-ID-TOKEN"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSLog(#"%#",request);
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"data dtat %#",data);
}];
[dataTask resume];
This url with parameters and headers returns data in the Postman. When I use to get data using objective c using the above code I get <42616420 52657175 6573740a> as data. And not calling the backend either.
After spending hours on this issue, I just sent the parameters via the URl and it returned data and printed the request in the backend. Code is as follows for anyone.
FIRDatabaseReference *agreementCreateReference = [[[FIRDatabase database] referenceWithPath:#"/agreements/"] childByAutoId];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *url = [NSString stringWithFormat:#"https://krib-api-onbit.herokuapp.com/api/agreements?agreementId=%#&listingId=%#",agreementCreateReference.key,_propertyId];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
[request setValue:idToken forHTTPHeaderField:#"X-FIREBASE-ID-TOKEN"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
//[request setHTTPBody:postData];
NSLog(#"%#",request);
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"data dtat %#",data);
NSString *res = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"res %#",res);
}];
[dataTask resume];

How to Retrive data from json post method through mvc controller web services using NSURLSESSION?

first of all please, click on this link then...
How I'm getting this output like name ,std & assign to textbox I'm already done this in xcode 5 but NSURLCOnnection not used in xcode 7.2 so Using NSURLSESSION How Can I bind to textbox??
NSError *error = nil;
NSMutableDictionary *dic2 = [[NSMutableDictionary alloc] init];
[dic2 setObject:#"324" forKey:#"grno"];
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setObject:#"RestAPI" forKey:#"interface"];
[dic setObject:#"StudentLogin" forKey:#"method"];
[dic setObject:dic2 forKey:#"parameters"];
NSData *postData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://ios.skyzon.in/STudent/STudentDetail"]];
[req setHTTPMethod:#"POST"];
[req setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[req setHTTPBody:postData];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:req
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"Response:%# %#\n", response, error);
if(error == nil)
{
// NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
// NSLog(#"Data = %#",text);
NSMutableDictionary *responseDic = [[NSMutableDictionary alloc]init];
responseDic = [NSJSONSerialization JSONObjectWithData:postData options:NSJSONReadingAllowFragments error:&error];
NSLog(#"%#",responseDic);
self.txt.text = [responseDic objectForKey:#"Name"];
NSLog(#"%#",[responseDic objectForKey:#"Name"]);
}
}];
[dataTask resume];
You can you NSURLSESSION like below.
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:#"http://ios.skyzon.in/STudent/STudentDetail"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"%#", json);
self.txt.text = [responseDic objectForKey:#"Name"];
}];
May be it will help you.

Send POST request in 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 (there is 4 pages))).
Every time one or two addresses are different and the other is the same, why is it? All of them should be different.
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];
for (int i=0; i<4; i++)
{
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: #"80", #"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++;
}

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

AFHTTPSessionManager post request

I want to post a request to server. It is working fine if I am using NSURLSessionDataTask. But underneath I need to use AFNetworking as my whole application is using it. But when I am trying to hit the same service in AFHTTPSessionManager with POST method, It is giving me request time out.
Below are both the codes.
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
NSURL *url = [NSURL URLWithString:#"BASE URL"];
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"];
NSString *postString = #"1";
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];
[postDataTask resume];
AFNetworking kit version :-
AFHTTPSessionManager *client = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:#"BASE URL"]];
NSDictionary *request = #{#"Content-Type":#"application/json",
#"Accept":#"application/json",
};
NSString *postString = #"1";
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[client POST:#"" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithHeaders:request body:data];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"%#",responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"%#",error);
}];
Please help me in implementing it in right way.
I solved this problem by creating my own request and give its configuration to AFURLSessionManager and then holding my own NSURLSessionUploadTask object.
Thanks