NSURLConnection Never finish - objective-c

i have the next code:
NSURL *url = [NSURL URLWithString:#"http:...."];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [""soapMessage"" length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [""soapMessage"" dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setTimeoutInterval:20];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[theConnection start];
Then sometimes, when i click a lot of times (Just to try where can fail the app) in my interface in the buttons assigned to start a new connection, the connection doesn't give a reply (I send the request but looks like is blocked after).
I would like to know what is the use of "setTimeoutInterval"? AND if there are some ways to limit the time to wait one response?
Thanks!

How do you know that the request started in the first place? You might be waiting for a response for a request that never started.
It appears you are not testing for theConnection being set to nil. also theRequest and url could also be nil if you run out of memory.

Related

Checking data sent in http post Request

Is it possible to check the data which I have sent in HTTP POST request, by logging the request in XCode.
Here is my code:
NSString *post = [NSString stringWithFormat:#"Firstname=%#&Lastname=%#&Email=%#",firstName,lastName,emailId];
NSLog(#"%#", post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"http://www.abcde.com/xyz/login.aspx"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
**NSLog(#"%#", request );**
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
I am logging the request as shown above in Bold. But here I am able to see only my Url and not any post data.
If it is not possible to see data here where can I see the data sent in http request.
If you want just check what data are you sending then I suggest you to use this awesome tool http://www.charlesproxy.com. It's very simple and powerful.
Just run Charles and do your request via simulator.

How to post data into a specific url or server

I want to build up a login page and the values of this page i want to submit in server through a specific link. can any one send the code that how i will send the data into server?
Here's an example using NSURLConnection to send POST parameters:
// Note that the URL is the "action" URL parameter from the form.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL #"http://www.whereq.com:8079/answer.m"]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send
NSString *postString = #"paperid=6&q77=2&q80=blah";
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:#"%u", [data length]] forHTTPHeaderField:#"Content-Length"];
[NSURLConnection connectionWithRequest:request delegate:self];

HTTP form POST method to server not sending email

Im trying to send a data or what it seems to a form that is on the website, the action is freecontactformprocess.php and my code goes like this,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.irabwah.com/freecontactformprocess.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
NSString *postString = [NSString stringWithFormat:#"Full_Name=%#&Email_Address=%#&Telephone_Number=%#&Your_Message=%#&AntiSpam=25",_fNameLabel.text, _emailLabel.text,_phoneLabel.text, _commentText.text];
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:#"%u", [data length]] forHTTPHeaderField:#"Content-Length"];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
if(conn){
NSLog("Successfull!!");
}
the purpose of this form is to send an email into my account which I integrated into the form on installation of this plugin, but the thing is I doesn't seems to get a the email when I run my test app, and when Im inputing values I don't include spaces yet.
The form looks like this,

How to send PostData request for NSMutableURLRequest

I have URL which takes a parameter. In Firefox. I have download add-on and find the parameter for the request. I found PostData Tab on the httpFox add-on tool and I saw the parameter and value.
I created a string and added the parameter, and encoded that string value
NSString *parameterId = [NSString StringWithFormat:#"ac$2_sdrex_es34_4333f=Caa111"];
NSString EncodeID = [parameterId
stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
...and created the PostData,
PostData = [encodeID dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
...and set the postLength,
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
...and set the http header values,
[request setValue:postLength forHttpHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
and then make the connection,
connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
But this results in the following error...
Error Domain = NSURLErrorDomain code= -1002 unsupported url
I have encoded the URL too.
Can anyone advise me how to resolve this issue?
This is what after creating the postData!
NSURL *url = [NSURL URLWithString:urlString];
theRequest = [[NSMutableURLRequest alloc]initWithURL:url];
[theRequest setValue:#"win-eqjf1b1ep06:20000" forHTTPHeaderField:#"Host"];
[theRequest setValue:#"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10" forHTTPHeaderField:#"User-Agent"];
[theRequest setValue:#"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" forHTTPHeaderField:#"Accept"];
[theRequest setValue:#"en-us,en;q=0.5" forHTTPHeaderField:#"Accept-Language"];
[theRequest setValue:#"gzip,deflate" forHTTPHeaderField:#"Accept-Encoding"];
[theRequest setValue:#"ISO-8859-1,utf-8;q=0.7,*;q=0.7" forHTTPHeaderField:#"Accept-Charset"];
[theRequest setValue:#"115" forHTTPHeaderField:#"Keep-Alive"];
[theRequest setValue:#"keep-alive" forHTTPHeaderField:#"Connection"];
[theRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPBody:postData];
#all
Thanks in advance
NSMutableURLRequest only supports http://, https://, ftp://, or file://
http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i
Are you using some other protocol?

Where to put Loading

In my app I have connections with a web service and I would like to create a loading view to show each time the connection starts until it returns a response.
Where is the best place to put this view in my application and what is the best way to implement it?
Thanks,
Fermin
NSURL *url = [NSURL URLWithString:#"http://.....?WSDL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:#"%d", [soapMessage length]];
[theRequest addValue: #"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[theRequest setHTTPMethod:#"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[theConnection start];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[webData appendData:data];
}