how to send post request with nsurlconnection - iphone

I have changed over to NSURLConnection and NSURLRequest delegates for my db connections etc.
I was using the ASIHTTPRequest libraries to do all this stuff but finally decided to move on due to the lack of support for that 3rd party library.
what I am woundering is how do I send post requests to my db like you do with the ASIFormDataRequest as shown below
//This sets up all other request
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setDelegate:self];
[request setValidatesSecureCertificate:NO];
[request setPostValue:#"ClientDataSet.xml" forKey:#"filename"];
[request startSynchronous];
i.e. how to do send the setPostValues with the NSURLRequest class?
any help would be greatly appreciated

Using NSURLRequest is slightly more difficult than utilizing ASIHTTPRequest. You have to build your own post body.
NSData *postBodyData = [NSData dataWithBytes: [postBodyString UTF8String] length:[postBodyString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:yourURL];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
[request setHTTPBody:postBodyData];

Related

How to post xml data in url as request?

I am developing an application in which i want to post
xml data as request but i am not able to post it correctly ,i think.
My request xml data is
<loginRequest><username>101</username></loginRequest>
and my request is as follows :
`NSString *post=#"<loginRequest><username>101</username></loginRequest>";
NSURL *url = [NSURL URLWithString:#"my url"];
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:#"POST"];
NSMutableData *mutData=[[NSMutableData alloc]init];
[request addRequestHeader:#"Content-Type" value:#"text/xml"];
[request setPostValue:#"test" forKey:#"body"];
[request setCompletionBlock:^{
NSData *data=[request responseData];
NSString *response=[request responseString];
}];
[request setFailedBlock:^{
NSLog(#"Failed");
}];
[request startAsynchronous];
`
Kindly help me with this..
You can check with your server api whether it supports different response type..
Accordingly you can set "Accept" parameter of HTTP request header.
[request addRequestHeader:#"Accept" value:#"application/xml"];

Set different callback with ASIHTTPRequest

For an app i'm currently making i use the ASIHTTPRequest API to do my communication:
NSURL *url = [NSURL URLWithString:#"http://testService.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
[request addRequestHeader:#"Content-Type" value:#"application/json"];
[request setTimeOutSeconds:20.0f];
[request setRequestMethod:#"POST"];
NSData * postData = [NSJSONSerialization dataWithJSONObject:dictionnary2 options:0 error:nil];
[request setPostLength:[postData length]];
[request appendPostData:postData];
[request setDelegate:self];
[request startAsynchronous];
i already have working calls in place but they both go to the same callback method :
- (void)requestFinished:(ASIHTTPRequest *)request
I want each call to have it's own callback method since i call one call from the callback method of the other. How can i do this ?
In this kind for situation i would always prefer to use Blocks for call backs.
Check this link for block implementation methods and design,

Is it necessary to create soap message when using ASIFormDataRequest/ASIHTTPRequest?

I used NSURLConnection to communicate with Soap webservice and it worked fine.
Now I am using ASIFormDataRequest and I am not getting is it necessary to create soap message for that? and I am not aware what is SoapAction.
Here is the code I am using
NSURL *url = [NSURL URLWithString:#"http://SERVERNAME.com/WEBSERVICENAME"];
[self setFrmdataReq:[ASIFormDataRequest requestWithURL:url]];
[frmdataReq setRequestMethod:#"POST"];
[frmdataReq addRequestHeader:#"Content-Type" value:#"text/xml; charset=utf-8"];
[frmdataReq setPostValue:txtField.text forKey:#"city"];
[frmdataReq setDelegate:self];
[frmdataReq setDidFinishSelector:#selector(requestFinished:)];
[frmdataReq setDidFailSelector:#selector(requestFailed:)];
[frmdataReq startSynchronous];
UPDATE
Soap Message I used,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *soapMessage = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\
<soap12:Body>\
<getCityTime xmlns=\"http://www.Nanonull.com/TimeService/\">\
<city>%#</city>\
</getCityTime>\
</soap12:Body>\
</soap12:Envelope>",txtField.text];
NSString *msgLength = [NSString stringWithFormat:#"%d",[soapMessage length]];
[request addValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[request addValue:msgLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn)
{
responseData = [[NSMutableData data]retain];
NSLog(#"connection successful");
}
else
NSLog(#"connection fail.");
I didn't defined SOAPAction as I don't know anything about that.
When using ASIHTTPRequest, you need to create the SOAP request in basically the same way as you do with NSURLConnection.
The setPostValue call in ASIHTTPRequest is for create HTTP POST requests in the same format use for POST <form> tags in HTML.
For comparing ASIHTTPRequest / NSURLConnection, see things like:
ASIHTTPRequest vs NSURLConnection
and "What is ASIHTTPRequest?" on:
http://allseeing-i.com/ASIHTTPRequest/

ios asihttprequest post string

I am using ASIHTTPRequest, its working fine for posting dictionaries to the web service in the body
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"first_name"];
but how can i use the
ASIFormDataRequest
to send a single string?
something like
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:token];
no value and key,just a string in the body,
thanks!
If you want to send data via POST but prefer to create the POST body yourself, use appendPostData: or appendPostDataFromFile:. The code goes something like this -
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[#"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];

Problem to post large amount of data to the web server in iphone

I am working in simple application in which I need to post data to the server using webservices. I post string data to the web server, but it gives an error "large amount of binary data will be truncated".
NSString *queryString = [NSString stringWithFormat:#"url/message=%#",strMessage];
queryString = [queryString
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:queryString];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[req addValue:0 forHTTPHeaderField:#"Content-Length"];
[req setHTTPMethod:#"GET"];
NSURLConnection *conn =
[[NSURLConnection alloc] initWithRequest:req delegate:self];
Any ideas?
Thanks in advance.
Did you try http://allseeing-i.com/ASIHTTPRequest/ ?
You can post any kind of datas. For example to send an image :
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Ben" forKey:#"first_name"];
[request setPostValue:#"Copsey" forKey:#"last_name"];
[request setFile:#"/Users/ben/Desktop/ben.jpg" forKey:#"photo"];
(from http://allseeing-i.com/ASIHTTPRequest/How-to-use)
Good Luck ! :-)