how to Convert the String to nsutf8 encode - ios

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

Related

How to pass data to webview in CCAvenue integration in ios?

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=%#&currency=%#",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];

how to pass string as parameter in post method

I am new to IOS i need to know how to pass NSString as parameter in nsurlconnection POST method,i passed string but it become empty
viewdidload:
NSString *parseURL =#"http:url";
NSString *encodeurl =[parseURL stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:encodeurl];
NSData *data = [NSData dataWithContentsOfURL:url];
if(data){
NSError *error;
NSDictionary *json1 = [NSJSONSerialization JSONObjectWithData:data options: kNilOptions error:&error];
arrMsg = [json1 valueForKeyPath:#"Branches.branch_name"];
arrmsg1 =[json1 valueForKeyPath:#"Branches.id"];
NSString *str = [arrmsg1 componentsJoinedByString:#","];
NSLog(#"%#",str);
[self sendDataToServer :#"POST"];
}
post method:
In post method i passed the string as parameter
-(void) sendDataToServer : (NSString *) method{
//NSString *str = [arrmsg1 componentsJoinedByString:#","];
NSString *post = [NSString stringWithFormat:#"branch_id=%#",str];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[post length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if( theConnection ){
// indicator.hidden = NO;
mutableData = [[NSMutableData alloc]init];
}
}
you can do like
arrMsg = [json1 valueForKeyPath:#"Branches.branch_name"];
arrmsg1 =[json1 valueForKeyPath:#"Branches.id"];
NSString *str = [arrmsg1 componentsJoinedByString:#","];
NSLog(#"%#",str);
// call the method like
[self sendDataToServer :#"POST" params:str];
// create the method like
-(void) sendDataToServer : (NSString *) method params:(NSString *)str{
NSString *post = [NSString stringWithFormat:#"branch_id=%#",str];
.... continue your work
}

how to convert nsaaray into nsstring using nsurlconnection Post method

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

iOS - How to send attachment using SOAP?

I am using NSMutableURLRequest to upload parameters to server using SOAP API and it is working fine.
static NSString *URLString = #"http://www.myURL";
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL URLString]];
[request addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPMethod:#"POST"];
[request addValue:#"http://..." forHTTPHeaderField:#"SOAPAction"];
NSString *name = #"John";
NSString *parameters = [NSString stringWithFormat:
#"<s:Envelope xmlns:s=\"http://..../\">\n"
"<s:Body >\n"
"<name>%#</name>\n"
"</s:Body>\n"
"</s:Envelope>\n", name];
[request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];
_myConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Now I want to send a text file (attachment) along with the parameters.
Can someone help?
Hi you can convert your file into NSDATA then into BASE64 String and add as parameter to your request body and after that at backend you can convert BASE64 String to Binary Data and save it as text file.
Encoding
NSString *str=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"file.txt"];
NSData *data = [NSData dataWithContentsOfFile:str];
NSString *base64String = [data base64EncodedStringWithOptions:0];
NSLog(#"%#", base64String);
Here is the link for BASE64 Conversion classes
Just add one more parameter
NSString *parameters = [NSString stringWithFormat:
#"<s:Envelope xmlns:s=\"http://..../\">\n"
"<s:Body >\n"
"<name>%#</name>\n"
"<fileData>%#</fileData>\n" //Here is new parameter
"</GetChanges>\n"
"</s:Body>\n"
"</s:Envelope>\n", name, base64String];

GET Request uses too many parameters- iOS

I'm trying to load a URL in NSMutableURLRequest for a GET request as below:
NSString *serverAddress = [NSString stringWithFormat:#"http://test.us.to:8080/api/offers?altId=%#&provider=%#&otherId=%#&tenantId=%#&createdAt=%#&otherOfferId=%#&postId=%#&pageId=%#&sourceUrl=%#&name=%#&description=%#&text=%#&category=%#&caption=%#&startTime=%#&expirationTime=%#&minPurchase=%#&numPurchases=%#&value=%#&percent=%#&count=%#&currency=%#&terms=%#&campaignId=%#&partnerId=%#&tenantIdAtPartner=%#&issuerName=%#&claimLimit=%#&onePerUser=%#&emailTemplateFile=%#",#"test",#"Facebook",#"",#"test",#"null",#"test",#"",#"",#"http://test.us.to/offers/harvester_summer13.html",#"Harvester",#"Harvester 2for1 TakeAway",#"Enjoy!",#"voucher",#"Harvester 2for1 TakeAway",[NSNumber numberWithInt:0],#"test",[NSNumber numberWithInt:0],[NSNumber numberWithInt:0],#"1000",[NSNumber numberWithInt:0],[NSNumber numberWithInt:0],#"GBP",#"",[NSNumber numberWithInt:1259],#"null" ,#"null" ,#"null",[NSNumber numberWithInt:100],[NSNumber numberWithBool:false],#"templates/test.vm"];
NSURL *url = [NSURL URLWithString:[serverAddress urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:15.0];
NSString *authStr = [NSString stringWithFormat:#"%#:%#", #"testuser", #"testpwd"];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:#"Basic %#", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:#"Authorization"];
[request setHTTPMethod: #"GET"];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(#"output data %#",response1);
I guess I'm loading way too many parameters using the url string. Is there a better way to pass parameters for a GET request ? The current output is null for response1
It looks to me like the issue is that you are encoding the whole url with some sort of encoding method, but you really just need to encode each parameter separately if it contains certain characters. For example:
NSString *serverAddress = #"http://test.us.to:8080/api/offers";
NSString *altIdParameter = [#"Escape?This?String?" urlEncodeUsingEncoding:NSUTF8StringEncoding];
NSString *aUrlParameter = [#"http://test.us.to/offers/harvester_summer13.html" urlEncodeUsingEncoding:NSUTF8StringEncoding];
NSString *getRequestUrl = [NSString stringWithFormat:%#?altId=%#&urlParam=%#", serverAddress, altIdParameter, aUrlParameter];
NSURL *url = [NSURL URLWithString:getRequestUrl];

Resources