I am using two links.First link i created nsaaray for "id".This "id" i need to pass as parameter in nsurlconnection using POST method by second link.i tried lots but i stuck in while passing parameter "id".
nsaaray for id:
- (IBAction)button_drop:(id)sender {
NSString *parseURL =#"first_link";
NSString *encodeurl =[parseURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodeurl];
NSData *data = [NSData dataWithContentsOfURL:url];
if(data){
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
arrMsg = [json valueForKeyPath:#"Branches.branch_name"];
arrmsg1 =[json valueForKeyPath:#"Branches.id"];
[_picker2 reloadAllComponents];
}
}
POST method using nsurlconnection:
NSString *Branchid=#"3";
NSURL *url = nil;
NSMutableURLRequest *request = nil;
if([method isEqualToString:#"GET"]){
NSString *getURL = [NSString stringWithFormat:#"%#?branch_id=%#\n", URL, Branchid];
url = [NSURL URLWithString: getURL];
request = [NSMutableURLRequest requestWithURL:url];
NSLog(#"%#",getURL);
}else{ // POST
NSString *parameter = [NSString stringWithFormat:#"branch_id=%#",Branchid];
NSData *parameterData = [parameter dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
url = [NSURL URLWithString: URL];
NSLog(#"%#", parameterData);
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPBody:parameterData];
arr= [NSMutableString stringWithUTF8String:[parameterData bytes]];
}
[request setHTTPMethod:method];
[request addValue: #"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if( connection )
{
mutableData = [NSMutableData new];
}
Try to create a "POST" method like this:-
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
Branchid, #"branch_id", nil];
NSString *newRequest = [dict JSONRepresentation];
NSData *requestData = [newRequest dataUsingEncoding:NSUTF8StringEncoding];
//If you are not using SBJsonParser Library then try the following:
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *urlString = #"YOUR URL String";
NSURL *requestURL = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%lu", (unsigned long) [requestData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:requestData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
mutableData = [NSMutableData new];
}
Also, You can follow this link if you don't find out the answer working properly:-
Data in POST or GET methods
Send data in POST methods
Related
NSString *phone = [NSString stringWithFormat:#"phone=%#",[paymentServerData valueForKey:#"phone"]];
NSString *key = [NSString stringWithFormat:#"key=%#",[paymentServerData valueForKey:#"key"]];
NSString *furl = [NSString stringWithFormat:#"furl=%#",[paymentServerData valueForKey:#"furl"]];
NSString *txnid = [NSString stringWithFormat:#"txnid=%#",[paymentServerData valueForKey:#"txnid"]];
NSString *productinfo =[NSString stringWithFormat:#"productinfo=%#",[paymentServerData valueForKey:#"txnid"]];
NSArray *arrayString = [[NSArray alloc] initWithObjects phone, key,furl,txnid,productinfoEncode,nil];
NSString *postString = [arrayString componentsJoinedByString:#"&"];
NSString *address = #"https://secure.payu.in/_payment";
NSURL *url = [NSURL URLWithString: address];
NSString *body = [NSString stringWithFormat: #"%#", postString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: body];
[_paymentWebView loadRequest: request];
i have 5 parameters to pass the body string but i want encode only one parameter how to encode one parameter and send the body method.
help me to encode this productionfo string.
// this line for encoding your data .
NSData *postData = [productinfo dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSURL *url = [NSURL URLWithString:#"URL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody:postData];
I'm new to CCAvenue integration and i want to pass all customer data to webview but i dont know how to pass it. can anyone help me to solve?
Try below code in viewDidLoad of CCAvenue webview Controller, here you can also pass billing address if you needed in your app.
//Getting RSA Key
NSString *rsaKeyDataStr = [NSString stringWithFormat:#"access_code=%#&order_id=%#",accessCode,orderId];
NSData *requestData = [NSData dataWithBytes: [rsaKeyDataStr UTF8String] length: [rsaKeyDataStr length]];
NSMutableURLRequest *rsaRequest = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: rsaKeyUrl]];
[rsaRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[rsaRequest setHTTPMethod: #"POST"];
[rsaRequest setHTTPBody: requestData];
NSData *rsaKeyData = [NSURLConnection sendSynchronousRequest: rsaRequest returningResponse: nil error: nil];
NSString *rsaKey = [[NSString alloc] initWithData:rsaKeyData encoding:NSASCIIStringEncoding];
rsaKey = [rsaKey stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
rsaKey = [NSString stringWithFormat:#"-----BEGIN PUBLIC KEY-----\n%#\n-----END PUBLIC KEY-----\n",rsaKey];
NSLog(#"%#",rsaKey);
//Encrypting Card Details
NSString *myRequestString = [NSString stringWithFormat:#"amount=%#¤cy=%#",amount,currency];
CCTool *ccTool = [[CCTool alloc] init];
NSString *encVal = [ccTool encryptRSA:myRequestString key:rsaKey];
encVal = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)encVal,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 ));
//Preparing for a webview call
NSString *urlAsString = [NSString stringWithFormat:#"https://secure.ccavenue.com/transaction/initTrans"];
NSString *Name=[NSString stringWithFormat:#"%# %#",billing_Fname,billing_Lname];
NSString *encryptedStr = [NSString stringWithFormat:#"merchant_id=%#&order_id=%#&redirect_url=%#&cancel_url=%#&enc_val=%#&access_code=%#&billing_name=%#&billing_address=%#&billing_city=%#&billing_state=%#&billing_zip=%#&billing_country=India&billing_tel=%#&billing_email=%#&delivery_name=%#&delivery_address=%#&delivery_city=%#&delivery_state=%#&delivery_zip=%#&delivery_country=India&delivery_tel=%#",merchantId,orderId,redirectUrl,cancelUrl,encVal,accessCode,Name,billing_address,billing_city,billing_state,billing_zip,billing_tel,billing_email,Name,billing_address,billing_city,billing_state,billing_zip,billing_tel];
NSData *myRequestData = [NSData dataWithBytes: [encryptedStr UTF8String] length: [encryptedStr length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: urlAsString]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setValue:urlAsString forHTTPHeaderField:#"Referer"];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: myRequestData];
[_viewWeb loadRequest:request];
I need to pass some data in order for the server to the save the info,in order to do this the parameters need to pass the parameters in XML nodes following a url http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=
Here is an example of the XML node structure
<Send_info>
<Service_order>
</Service_order>
<Data_stream>
<info>
</info>
</Data_stream>
</Send_info>.
The return value would be a JSON string with {"Uploaded":"TRUE"} or {"Uploaded":"FALSE"} respectively.
I have tried this method with no answer it just return nil.
-(void)Request:(NSData*)data{
NSURL *aUrl = [NSURL URLWithString:#"http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML="];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:#"GET"];
[request setHTTPBody:data];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
edit:you can find the answer in the comments
The answer was quite simple just use these blocks of code and substitute them with your own parameters
POST:
-(void)webservicepost:(NSString *)poststring{
NSString *poststring = #"String in the format we need to upload";
NSURL *remoteURL = [NSURL URLWithString:#"URL in which we'll upload"];
NSMutableURLRequest *Request = [[NSMutableURLRequest alloc] initWithURL:remoteURL];
[Request addValue:#"application/json; charset=utf-8" forHTTPHeaderField: #"Content-Type"];//Formats for the data
NSMutableData *body = [NSMutableData data];
[body appendData:[poststring dataUsingEncoding:NSUTF8StringEncoding]]; //Add content and coding
[Request setHTTPMethod:#"POST"]; //POST method
[Request setHTTPBody:body];
NSData *data = [NSURLConnection sendSynchronousRequest:Request returningResponse:nil error:nil];//Server response in data form
NSString *sdata = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//Server response in string form
}
GET:
-(void)get:(NSString *)myxmlstring{
NSString *escapedString = [myxmlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"http://xxx.xxx.xx.xxx/WCF/Service1.svc/XXX/Upload?sXML=%#",escapedString];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSURLConnection sendSynchronousRequest:Request returningResponse:nil error:nil];//Server response in data form
NSString *sdata = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//Server response in string form
//FROM HERE YOU CAN USE THE RETRIEVED INFORMATION FROM THE URL
}
I'm sending a POST request to server but they receive it duplicated with the same date and even time. this is my code :
-(void) getTerms{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
User *mUser = [[User alloc]init];
NSString *appFile1 = [self getUserFile];
mUser = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile1];
NSString *post = [NSString stringWithFormat:#"Method=GetFAQ&CustomerID=%#",mUser.customerID];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[post length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[ServerName serverName]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
if( theConnection ){
// other codes
}
});
});
}
I just wonder what is wrong with my code ? thanks in advance
this is how i solved the problem :
-(void) getCredit{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *post = [NSString stringWithFormat:#"Method=%#",mMethod];
NSMutableURLRequest *request = [ServerName postToServer:post];
dispatch_async(dispatch_get_main_queue(), ^{
NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];
NSString *jsonString = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSUTF8StringEncoding];
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSDictionary *res1 = [res objectForKey:#"ServerResponse"];
});
});
}
i have tried to make login form to check Username & Password from external server using http request but the response is given <>
- (IBAction)getlogin:(UIButton *)sender {
NSString *rawStrusername = [NSString stringWithFormat:#"username=%#",_username.text];
NSString *rawStrpassword = [NSString stringWithFormat:#"password=%#",_password.text];
NSString *post = #"rawStrusername&rawStrpassword";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
/*
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];*/
NSURL *url = [NSURL URLWithString:#"http://www.othaimmarkets.com/my_services_path/user/login"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
/*[request setValue:postLength forHTTPHeaderField:#"Content-Length"];*/
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(#"responseData: %#", responseData);
}
any help, suggestions or examples would be appreciated
NSString *post = #"rawStrusername&rawStrpassword";
You are sending the "rawStrusername&rawStrpassword" string to the server? Why don't you try with:
NSString *post = [NSString stringWithFormat:#"%#&%#", rawStrusername, rawStrpassword];