I need to send coordinates to a web server in each five minutes. I'm doing this way:
In the -(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation I have variables (declared in the .h file) receiving the information I want to send by the post.
And I have a NSTimer calling a method wich initiates this:
{
ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL:myURL];
[request setDelegate:self];
[request setPostValue:myValue forKey:#"myKey"];
[request addRequestHeader:#"Content-Type" value:[NSString stringWithFormat:#"application/x-www-form-urlencoded; charset=%#", #"UTF-8"]];
[request startAsynchronous];
}
The app crashes when the timer calls the function to send data.
Anyone, please, can help me?
Based on what code you've given us, the only place the problem can be is in the serialization of myValue. Since we have no idea what that object is, we can't do much other than make broad suggestions. myValue needs to be a string, encoded such that it is safe to submit as a form value. URL encoded, XML, or even JSON would work. RestKit and ObjectiveResource both have good serializer implementations you could base on.
The important thing to realize is that you can't add an arbitrary object to the post values of ASIFormDataRequest unless you've built some plumbing around your objects and ASI* to make it format your objects for HTTP.
Related
As soon as, I send request to the server (via NSURLConnection sendSynchronousRequest method), the server receives it in about 2 seconds, it processes and sends back response in another 3-5 seconds. However, I only get back the response in 30-35 seconds. This delay makes our communication very slow.
Even the async APIs are getting a delayed response.
Earlier, everything was working fine, with client getting the response back within 10 seconds.
Anyone else having this issue? What could be the reason?
EDIT
here is a screenshot of Wireshark analysis:
Link to a better image
How should I see what packet is saying what?..and why is it getting delayed?
EDIT2
Here is the code:
NSHTTPURLResponse *response=nil;
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:nsURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:180.0];
[theRequest setHTTPMethod:#"POST"];
[theRequest setTimeoutInterval:180.0];
[theRequest setHTTPBody:[[NSString stringWithFormat:#"%#",sdata] dataUsingEncoding:NSASCIIStringEncoding]];
NSError *error= nil;
NSData *result = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
if (error ) {
NSLog(#"error sending synchronous request: %#", error);
}
NSLog(#"request completed with code:%d",response.statusCode);
I'd avoid calling sendSynchronousRequest. Use the asynchronous version instead if you're not doing the call already on a background thread (you don't want to block the UI thread).
How do you know when the iOS response is received? An NSLog? A UI state change?
See also these questions:
NSURLConnection sendSynchronousRequest - background to foreground
NSURLConnection sendSynchronousRequest taking too much time to respond
Update
If you're a bit stuck, one strategy might be to rule out the use of NSURLConnection as the problem.
Strategy 1: try using NSURL's asychronous connection call instead of synchronous
Strategy 2: try using a different HTTP lib, such as AFNetworking
If you want to take a closer look at what is going on with the HTTP connection, you can use tools such as Charles, Fiddler or Wireshark to debug what data is being sent and received. To get the most benefit from this sort of analysis, you need to have some knowledge of the HTTP protocol(s). This is probably more time consuming than the previously mentioned strategies.
See also questions such as How to monitor network calls made from iOS Simulator.
Update
Are you accessing a webserver of your own, or is it someone else's?
Have you had a close look at the headers being sent to your webserver (and the ones being returned)? Pay attention to the content length, for example. Wrong content length can cause a delay, as explained here.
To see the request and returned headers, you could use Firebug, or something like wget or curl on the command line.
Also, double check that there's not a newline on the end of your URL, as described here.
Solution that worked for me:
In the request headers, iOS sets "gzip" for "Accept-Encoding" by default. The gzip compression was taking a lot of time, and hence the delayed response. I did the following to solve the problem:
[theRequest setValue:#"" forHTTPHeaderField:#"Accept-Encoding"];
NOTE: Check your headers, for any response delay.
Thanks to #occulus for directing me to the request headers!
You do this over wifi or celluar?
Speaking from my experience, when my data useage is exeeded my phone-provider slows down my downloads. Sometimes this happen to me at the end of the month after I did use a lot of mobile data.
I am not sure to say but it may be network problem first check it. it proper or down ?? okay Maybe I will be wrong.. but first check it...
Following describe code it might solve your problem :)
NSString *url = #"Your URL ";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
.
.
/// Add Here Your NSMutableData Valuew
.
.
[request setHTTPBody:body];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection)
{
self.responseData = [[NSMutableData alloc] init];
}
else
NSLog(#"Connection Failed!");
To ease your problems with asynchronous HTTP Request you should consider using the AFNetworking framework
In my iOS app, I'm using a UIWebView and a custom protocol (with my own NSURLProtocol implementation). I've been fairly careful about making sure that whenever I load a url, I load something like this into my UIWebView:
myprotocol://myserver/mypath
and in my NSURLProtocol implementation, I take a mutable copy of the NSURLRequest, convert the URL to http: and send that to my server.
Everything works for HTTP GET requests. The problem I encounter is with POST requests. It seems like the UIWebView doesn't properly encode the form data in the HTTPBody if the request uses my custom protocol.
One work-around, since I'm using HTTPS for my server requests, is that I register my protocol handler to intercept http: instead of myprotocol: and I can convert all calls to https: This other question, here, pointed me toward that solution:
But I'm wondering if there's any alternative and/or better way of accomplishing what I want.
Instead of trying to use POST requests, one work around is to continue using GET requests for myprotocol:// URLs, but transform them in your NSURLProtocol implementation to an http:// and POST request to your server using the request query string as the body of the POST.
The worry with using GET requests to send large amounts of data is that somewhere along the request chain, the request line might get truncated. This appears to not be a problem, however, with locally-implemented protocols.
I wrote a short Cordova test app to experiment and I found that I was able to send through a little over 1 MiB of data without trouble to the HTTP request echoing service http://http-echo.jgate.de/
Here is my startLoading implementation:
- (void)startLoading {
NSURL *url = [[self request] URL];
NSString *query = [url query];
// Create a copy of `url` without the query string.
url = [[[NSURL alloc] initWithScheme:#"http" host:#"http-echo.jgate.de" path:[url path]] autorelease];
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:url];
[newRequest setHTTPMethod:#"POST"];
[newRequest setAllHTTPHeaderFields:[[self request] allHTTPHeaderFields]];
[newRequest addValue:#"close" forHTTPHeaderField:#"Connection"];
[newRequest addValue:#"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
[newRequest setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];
urlConnection = [[NSURLConnection alloc] initWithRequest:newRequest delegate:self];
if (urlConnection) {
receivedData = [[NSMutableData data] retain];
}
}
I then implemented the NSURLConnection protocol methods to forward to the appropriate NSURLProtocolClient method, but building up the response data in the case of Transfer-Encoding:chunked (as is the case for responses from http://http-echo.jgate.de/).
Unfortunately it looks like that http: and https: scheme requests are handled slightly differently than other (including custom) schemes by Foundation Framework. Obviously HTTPBody and HTTPBodyStream calls on relevant NSURLRequest returns always nil for former ones. This is decided already prior call of [NSURLProtocol canInitWithRequest] therefore custom NSURLProtocol implementation has no way of influencing that (it is too late).
It seems that different NSURLRequest class is used for http: and https: than 'a default one'. Default GnuStep implementation of this class returns always nil from HTTPBody and HTTPBodyStream calls. Therefore particular implementations (e.g. one under PhoneGap, likely part of Foundation Framework) choose NSURLRequest-type of class based on scheme prior consulting that with NSURLProtocol. For custom schemes, you get NSURLRequest that returns nil for both HTTPBody and HTTPBodyStream which effectively disables use of POST method (and other methods with body) in custom URI scheme handler.
Maybe there is a way how to influence decision of which NSURLRequest class is actually used but it is currently unknown to me.
As a workaround, you can still use http: or https: scheme and decide in [NSURLProtocol canInitWithRequest] based on other criteria (e.g. host name).
I am working in a iPhone project that uses restful web services. I need to send some tracking code or version number with every request sending from iPhone side. I am thinking of adding a header value into every request, by modifying a ASIHTTPRequest class.
Is there any method that ASIHTTPRequest providing to do this (without modifying framework files)?
If I need to modify ASIHTTPRequest class, what is the best place to modify it? (like buildRequestHeaders.. )
Thanks.
may be you can write a new class such as MyASIHttprequest and override the method requestwithURL
+ (MyASIHTTPrequest *) requestWithURL:url{
MyASIHTTPrequest *request;
if (request = [super requestWithUrl:url]){
[request addRequestHeader:#"" value:#""];
}
return request;
}
or you can write a method to add the header but maybe every time you new a asihttprequest, you should call this method to add header;
You can add headers by following method
ASIFormDataRequest *currentRequest = [ASIFormDataRequest requestWithURL:url];
[currentRequest addRequestHeader:#"" value:#""];
e.g.[currentRequest addRequestHeader:#"Content-Type" value:#"text/xml; charset=utf-8"];
Hope this helps.
If you are just starting by adding in ASIHTTPRequest and it is not embedded into your app yet then I recommend that you stop and switch to something else.
The creator of ASIHTTPRequest has discontinued development on the framework and even recommends in his blog post "Honestly, I think now is the time to start looking elsewhere." -Blog post
Also this was addressed in this question as well: Is it safe to still use ASIHTTPRequest?
I would personally recommend that you use NSURLConnection and send the data through the POST method.
iam using to download multiple file using by pass ASIHTTPRequest to operation queue...
NSInvocationOperation *operation =[[NSInvocationOperation alloc]initWithTarget:self selector:#selector(DownloadFile:) object:url];
.
.
.
-(void)DownloadFile:(NSURL)url{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadProgressDelegate:self];
[request setDidFailSelector:#selector(requestWentWrong:)];
[request setDidFinishSelector:#selector(requestFinished:)];
[request setDelegate:self];
[request startSynchronous];
}
- (void)setProgress:(float)progress{
NSLog(#"Current progress %f :",progress);
}
The progress is working fine but i can't know from which URL or from which operation..
I want to know how to get each download progress of each url individually...
and how i cancel each one not cancel all operations.
Thanks
First an answer to your question: how about you create one "delegate" object for each download? Then it's obvious how download progresses for each download and cancel is easy, too. The code will be more complicated, though.
Therefore I have another suggestion: the author of ASIHTTPRequest library has stopped developing the library, so you might switch to something else. He's suggestion for example AFNetworking, but many people recommend nowadays MKNetworkKit. It seems to have pretty good queue handling.
Note the signature of the delegate messages: Each one takes an argument. That argument is the request sending you the message: The request, when it sends you a delegate message, includes itself among the arguments so that you know which request has gotten that far.
I am creating my first iPad app. I have a web application that I would like to authenticate against and pull data from in a RESTful way.
If you open up the URL in the browser (https://myapp.com/auth/login), you will get a form to enter your username and password. I was thinking I could set the login credentials in the post data of the request and submit the data.
The site uses HTTPS for login so that credentials aren't passed in plain text over the internet.
How can I make a secure HTTPS connection to pass credentials? Will this remember that I am logged in for future requests? What is the best way to do this?
Further update, October 2013
Although at the time I wrote this answer, ASIHTTPRequest was maintained a widely supported, this is no longer the case. It is not recommended for new projects - instead use NSURLConnection directly, or use AFNetworking.
With AFNetworking, there is a [httpClient setAuthorizationHeaderWithUsername:username password:password]; method for http authentication, and create a POST style form is equally easy - see AFNetworking Post Request for that.
Original answer:
A lot of people use the ASIHTTPRequest class to deal with http & https on the iPhone/iPad, as it has a lot of useful features that are difficult or time consuming to achieve with the built in classes:
http://allseeing-i.com/ASIHTTPRequest/
Starting at the simplest level you'd start with something like:
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(#"response = %#", response);
}
If you're using HTTP authentication, ASIHTTPRequest will automatically prompt the user for the username and password.
IF you're using some other form of authentication you probably need to request the username and password from the user yourself, and submit them as a POST value or a custom http header, and then the response may either include a token in a JSON or XML response, or it could set a cookie.
If you add more details as to how the authentication scheme works I can be a bit more specific.
Update
Based on the update, to emulate a POST form you'd just need to add lines like:
[request addPostValue:usernameString forKey:#"username"];
[request addPostValue:passwordString forKey:#"password"];
You will also need to change the way you create the request, instead of:
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
do:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
(I also forget to mention it above, there's code I put earlier is using a synchronous request, so you'd want to replace it with an asynchronous one to avoid blocking the UI once you'd proved it was working.)
There's a JSON framework for the iphone here:
http://code.google.com/p/json-framework/
which works well for me and is simple to use with ASIHTTPRequest.