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

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/

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

Cant Post To Server Well

i am trying to post to a server in the following code.
i cant receive the response that i need to get.
think i am making something wrong, need some help
NSString *uuid = #"acdede9b-6fa6-44d0-8837-cd11771139be";
NSString *rec = #"ewoJInNpZ25hdHVyZSIgPSAiQWphZElyM2hFclc2L0xReDYwNGtpZ3FpZmxZUll4MTdJUUN1VUQ3UG1LS2tCNU1yeUxvU2NOejdnKzVlYnZBS001TnV2R3dOVWRmcmFEeHN3eElONVJXZXFoc1Q0OEJMUVlXNzBnWmFyTUhVbW81SW1BdFVvZy9PWlQyTkVvdW5DNW5uR3JBQll0K0Q2VFdZK1hEL0NIczczcmpqcmtONnNFalNmR05kNEg4K0FBQURWekNDQTFNd2dnSTdvQU1DQVFJQ0NHVVVrVTNaV0FTMU1BMEdDU3FHU0liM0RRRUJCUVVBTUg4eEN6QUpCZ05WQkFZVEFsVlRNUk13RVFZRFZRUUtEQXBCY0hCc1pTQk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TG1Gd2NHTnBaR1Z1ZEVGd2NDSTdDZ2tpWW5aeWN5SWdQU0FpTVM0d0lqc0tmUT09IjsKCSJlbnZpcm9ubWVudCIgPSAiU2FuZGJveCI7CgkicG9kIiA9ICIxMDAiOwoJInNpZ25pbmctc3RhdHVzIiA9ICIwIjsKfQ==";
NSString *ServerContent = [NSString stringWithFormat:#"registerRecipt.aspx?udid=%#&receipt=%#",uuid,rec];
NSData *requestdata = [ServerContent dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"myserverurl"]];
request.timeoutInterval = 20;
[request setHTTPMethod:#"POST"];
[request setHTTPBody:requestdata];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
error
The page cannot be displayed Open IIS Help, which is accessible in IIS Manager (inetmgr),
Try setting the Content-Length header:
[request setValue:[NSString stringWithFormat:#"%d", [requestData length]]
forHTTPHeaderField:#"Content-Length"];

ASIHTTPRequest wrapping JSON in quotes

I'm using ASIHTTPRequest and json-framework to post JSON to a rails app,
The problem is that when the JSON arrives on the server it is wrapped in double quotes,
Here is the code i am using to encode and send the JSON:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#/shared_lists.json",kAPIRoot]]];
NSString *dataString = [NSString stringWithFormat:#"shared_items=%#&shared_list=%#&facebook_id=%#",[sharedItems JSONRepresentation],[sharedList JSONRepresentation],facebookID];
NSLog(#"%#",[NSString stringWithUTF8String:[[dataString dataUsingEncoding:NSUTF8StringEncoding] bytes]]);
[request appendPostData:[dataString dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
[queue addOperation:request];
Which logs correctly as:
shared_items=[{"entity_id":"531","position":"1"},{"entity_id":"733","position":"2"},{"entity_id":"723","position":"3"},{"entity_id":"2530","position":"4"}]&shared_list={"list_id":1197}&facebook_id=-1540981104
Although when it arrives on the server it outputs as:
shared_items="[{"entity_id":"531","position":"1"},{"entity_id":"733","position":"2"},{"entity_id":"723","position":"3"},{"entity_id":"2530","position":"4"}]",shared_list="{"list_id":1197}",facebook_id="-1540981104"
How do i stop the arrays being wrapped up as a single string?
Try this way:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#/shared_lists.json",kAPIRoot]]];
NSString *dataString = [NSString stringWithFormat:#"shared_items=%#&shared_list=%#&facebook_id=%#",[sharedItems JSONRepresentation],[sharedList JSONRepresentation],facebookID];
NSMutableData *requestBody = [[NSMutableData alloc] initWithData:[dataString dataUsingEncoding:NSUTF8StringEncoding]];
[request setRequestMethod:#"POST"];
[request setPostBody:requestBody];
[request addRequestHeader:#"Content-Type" value:#"application/json; encoding=utf-8"];
[request setUseSessionPersistence:NO];
[request setUseCookiePersistence:NO];
[request setCacheStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
I solved the problem by manually creating the JSON String like so:
NSString *dataString = [NSString stringWithFormat:#"{\"shared_items\":%#,\"shared_list\":%#,\"facebook_id\":%#}",[sharedItems JSONRepresentation],[sharedList JSONRepresentation],facebookID];

Problems with using POST from iPhone to REST service

I'm building an iPhone client that will GET and POST to a REST service.
The GET-part works fine, but I don't seem to be able to POST anything. The server only accepts xml at the moment.
I've tried to use ASIHTTPRequest, ASIFormDataRequest and doing it myself but nothing seems to work.
Can anyone help me? I'm fairly new to Objective-C programming.
I do get a status code: 200 as a response using the following code in didReceiveResponse:
NSHTTPURLResponse * httpResponse;
httpResponse = (NSHTTPURLResponse *) response;
int myStatus = httpResponse.statusCode;
if (myStatus == 400) {
NSLog(#"Status code: 400");
} else if (myStatus == 200) {
NSLog(#"Status code: 200");
} else {
NSLog(#"Other status code");
Here follows three different approaches...
Code (ASIHTTPRequest):
NSURL *url = [NSURL URLWithString:#"myUrl.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request appendPostData:[#"<my xml>" dataUsingEncoding:NSUTF8StringEncoding]];
// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request startSynchronous];
Code (ASIFormDataRequest):
NSURL *url = [NSURL URLWithString:#"someUrl.com"];
ASIFormDataRequest *theRequest = [ASIFormDataRequest requestWithURL:url];
// [theRequest setPostValue:#"" forKey:#"key1"];
[theRequest setPostValue:#"90" forKey:#"key2"];
[theRequest setPostValue:#"150" forKey:#"key3"];
// [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
[theRequest startAsynchronous];
Code (Third method):
NSData *myPostData = [[NSString stringWithFormat:#"<my xml>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
// NSString *xmlPostData = #"<my xml>";
NSURL *theURL = [NSURL URLWithString:#"someUrl.com"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];
[theRequest setHTTPMethod:#"POST"];
[theRequest setValue:#"application/xml; charset=utf-8" forHTTPHeaderField:#"Content-type"];
// [theRequest setValue:#"UTF-8" forHTTPHeaderField:#"Charset"];
[theRequest setHTTPBody:myPostData];// [xmlPostData dataUsingEncoding:NSUTF8StringEncoding]];// myPostData];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
How about all these -(void)connection: style functions? Does any of them have to contain specific code for the connection to work?
Many thanks for any help!
NSURL *url = [NSURL URLWithString:#"http://myURL.com"];
ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url];
NSData *myPostData = [[NSString stringWithFormat:#"<My XML Body>"] dataUsingEncoding:NSUTF8StringEncoding];//NSASCIIStringEncoding];
NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];
[request addRequestHeader:#"Content-Type" value:#"application/xml"];
[request setPostBody:myMutablePostData];
[request setDelegate:self];
[request startSynchronous];

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 ! :-)