I have the following requirement to call a service using an ASIHTTPRequest with a POST method call. When I execute the request I get the following error:
errorError Domain=ASIHTTPRequestErrorDomain Code=5 "Unable to create request (bad url?)" UserInfo=0x790b490 {NSLocalizedDescription=Unable to create request (bad url?)}
And heres my code:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:SampleURL]];
[request addPostValue:#"emailid" forKey:username.text];
[request addPostValue:#"password" forKey:password.text];
[request addPostValue:#"first_name" forKey:first_name];
[request addPostValue:#"last_name" forKey:last_name];
[request addPostValue:#"option" forKey:#"userSignup"];
request.delegate = self;
[request startSynchronous];
Can anyone help me on this?
NSString *safestring=[strUrl stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url= [NSURL URLWithString:safestring];
[request addPostValue:#"emailid" forKey:username.text];
Shouldn't that be in the opposite order: [request addPostValue:username.text forKey:#"emailid"];
Same for the rest...
It looks like that 'sampleURL' has some issue. What is the data type of sampleUL? it should be NSURL.
Also what is in sampleURL when this piece of code is executed.
Related
I want to upload (POST) image to server with setkey "FileData" like before (wrote with ASIHTTPRequest and it works)
[self.request setData:imageData withFileName:dateFormatted andContentType:#"image/jpeg" forKey:#"Filedata"];
with AFNetworking I set like below:
NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:#"POST" path:#"" parameters:paramsDic constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:#"Filedata" fileName:dateFormatted mimeType:#"image/jpeg"];
}];
seems name not actually works...
How should I set the key? #mattt
Thanks
I figure it out, the problem is I add this line
[afRequest setValue:#"application/x-www-form-urlencoded; charset=UTF8" forHTTPHeaderField:#"Content-Type"];
But I still don't know why, I add this line to other http request and it works, but file upload. I checked server, when do file upload with this line, server received many ... hm... come to think how to describe it, mass letters ...like below:
Content-Type: image/jpeg^M ^M
ÿØÿà^#^PJFIF^#^A^A^#^#^A^#^A^#^#ÿá^#XExif^#^#MM^#*^#^#^#^H^#^B^A^R^#^C^#^#^#^A^#^A^#^#<87>i^#^D^#^#^#^A^#^#^#
hi i am uploading audio/image file as below please check it and its working fine using the ASIHTTPRequest.
NSURL *audiourl = [NSURL URLWithString:#"Your Url"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:audiourl];
NSData *postData = [NSData dataWithContentsOfURL:SoundPath];
//SoundPath is your audio url path of NSDocumentDirectory.
[request addData:postData withFileName:#"mynewfile.png" andContentType:#"image/png" forKey:#"FileData"];
[request setDelegate:self];
[request startasynchronous];
Thanks
I am currently working on an application I need to receive the data in order its very important so instead of going with asynchronous I am using synchronous. However this introduces a very unfortunate side effect, the synchronous request locks up the UI thread.
What I am doing to combat this issue is introduce Multithreading into my app with the use of the life saving "Grand Central Dispatch" services, which seems to be very easy to get my head around so far.
So with all this in mind I am having an issue with what I am doing, Previously I was using asynchronous and everything worked sweet, changing that to synchronous gives me this error
Error Domain=ASIHTTPRequestErrorDomain Code=1 "A connection failure occurred" UserInfo=0x68052a0 {NSUnderlyingError=0x683d250 "The operation couldn’t be completed. Connection refused", NSLocalizedDescription=A connection failure occurred}
Heres my code so far.
- (IBAction)setRequestString:(NSString *)string
{
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:#"http://192.168.1.1:8778/Data/"]; // iphone development
//PHP file name is being set from the parent view
[databaseURL appendString:string];
//call ASIHTTP delegates (Used to connect to database)
NSURL *url = [NSURL URLWithString:databaseURL];
//Used to Check which ASI cache to use (also used in requestFinished
xmlFileName = [[NSString alloc] initWithFormat:string];
//Set up multithread with GCD
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
//Create If statments here to set up the different caches to be passed down to the next view
if ([string isEqualToString:#"high.xml"]){
//Cache stuff goes in here
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days - this will change to cache until DBVersion changes
[request setDelegate:self]; // this calls the delegate function requestFinished
dispatch_sync(queue, ^ {
[request startSynchronous];
});
}else if ([string isEqualToString:#"low.xml"]){
//Cache stuff goes in here
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setSecondsToCache:60*60*24*30]; // Cache for 30 days - this will change to cache until DBVersion changes
[request setDelegate:self]; // this calls the delegate function requestFinished
dispatch_sync(queue, ^ {
[request startSynchronous];
});
}
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
//.... etc
hopefully that gives you a better idea of what im trying to do, I think maybe I am missing something with the way I am declaring my syncronious start.. as in the asihttprequest help file they say to declare it like this
- (IBAction)grabURL:(id)sender
{
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];
}
}
however Im working with data.. so this line
NSString *response = [request responseString];
will not work? dose it need to be NSData.. etc I dunno if someone could help me out that would be great.
You can use nsoperationqueue...
you can create one NSoperationInvoke and add those to NSoperationQueue in order(after reciving data sending another request)...You can add observer to NSOperationQueue to ensure that how many request will process at a time...in your case it will be just one...after receiving the notification in the observer that the synchronous process is completed it will call a selector by performonMainThread for starting another request in the observer itself...
on NSString *response = [request responseString];
issue you can check the request object by [request iskindofClass:[ClassName class]];
or nslog("%#",[request describe]);
it will tell what kind of object request is
Have you considered just adding a serial queue to your code?
dispatch_queue_t queue = dispatch_queue_create("com.myapp", NULL);
You are using a concurrent thread and it's causing multiple operations to occur at the same time. Also, wrap properly your ASIHttpRequest code within the queue blocks.
Give that a try and let us know
i am new to iphone....when i need to connect to one of the web service i am using follwing code tat is using ASIHTTP request..when i add this code its coming error
ASIHTTPRequest is undeclared declare it first where i shd declare and wat i shd declare?i am using UIViewController class
how i shd declare? wat i shd do?plz suggest me
the code is given below
-(void)callWebService
{
//this is a typical url for REST webservice, where you can specify the method that you want to call and the parameters directly with GET
NSURL *url = [NSURL URLWithString:#"http://www.yourserver.net/webservice/rest/?method=myMethod&par1=ok"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDidFinishSelector:#selector(requestCompleted:)];
[request setDidFailSelector:#selector(requestError:)];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestCompleted:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
}
- (void)requestError:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
thank u...
First check that you have asihttp package attached in your project.
After that check you have
#import "ASIHTTPRequest.h" import this in your viewcontroller
Dont forget to check that you have attached the following frameworks. CFNetwork, SystemConfiguration, MobileCoreServices, CoreGraphics and libz framework.
You have not tell which method to use
[request setRequestMethod:#"GET"]; BEFORE [request startAsynchronous];
When you will right click on framework folder this popup will appear.
I'm doing an app which retrieves user info, and it does that using a GET request, but I don't know how could I make one. Already tried ASIHTTPRequest with this code:
NSURL *url = [NSURL URLWithString:#"http://forrst.com/api/v2/users/info"];
// Now, set up the post data:
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:#"1" forKey:#"id"];
[request setRequestMethod:#"GET"];
// Initiate the WebService request
[request setDelegate:self];
[request startAsynchronous];
But I get this response from forrst:
2011-06-05 16:59:32.189 Forrsty[4335:207] {"resp":{"error":"you must pass either id or username"},"stat":"fail","in":0.0557,"authed":false,"authed_as":false,"env":"prod"}
Which I understand I'm not doing the GET request ok, so how I would do it? Thanks :)
A Get Parameter ?
So why don't you try "http://forrst.com/api/v2/users/info?id=1" ?
[ NSString stringWithFormat:#"http://forrst.com/api/v2/users/info?id=%d", 1 ];
By the way, take a look at this librayry : http://allseeing-i.com/ASIHTTPRequest/
Good luck !
I am just trying to fetch the Preapproval Key from PayPal server right from my iPhone App instead of setting up a Separate server for that. (For Testing Purpose). Can this be achieved?
I have used the following code to do that:
NSString *url = #"requestEnvelope.errorLanguage=en_US&cancelUrl=http://www.bytelyte.com/PayPal_X_NVP_tester.php?cmd=test¤cyCode=USD&endingDate=27.05.11 &maxAmountPerPayment=5&maxNumberOfPayments=2&maxTotalAmountOfAllPayments=5&pinType=NOT_REQUIRED&returnUrl=http://www.bytelyte.com/PayPal_X_NVP_tester.php?cmd=test&startingDate=27.01.11&senderEmail=krish_1297240918_per#gmail.com//www.bytelyte.com/PayPal_X_NVP_tester.php?cmd=test&startingDate=27.01.11&senderEmail=krish_1297240918_per#gmail.com";
NSData *postData = [url dataUsingEncoding:NSASCIIStringEncodinallowLossyConversion:YES];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[request setTimeoutInterval:1.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"XXYYZZZZ" forHTTPHeaderField:#"X-PAYPAL-SECURITY-USERID"];
[request setValue:#"XXYYZZZZ" forHTTPHeaderField:#"X-PAYPAL-SECURITY-PASSWORD"];
[request setValue:#"XXYYZZZZ" forHTTPHeaderField:#"X-PAYPAL-SECURITY-SIGNATURE"];
[request setValue:#"NV" forHTTPHeaderField:#"X-PAYPAL-REQUEST-DATA-FORMAT"];
[request setValue:#"NV" forHTTPHeaderField:#"X-PAYPAL-RESPONSE-DATA-FORMAT"];
[request setValue:#"127.0.0.1" forHTTPHeaderField:#"X-PAYPAL-DEVICE-IPADDRESS"];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"utf-8" forHTTPHeaderField:#"charset"];
The Response I have got is the following:
responseEnvelope.timestamp=2011-02-09T05%3A02%3A38.859-08%3A00&responseEnvelope.ack=Failure&responseEnvelope.correlationId=981f044262212&responseEnvelope.build=1655692&error(0).errorId=560029&error(0).domain=PLATFORM&error(0).severity=Error&error(0).category=Application&error(0).message=The+required+X-PAYPAL-APPLICATION-ID+header+is+missing+from+the+HTTP+request&error(0).parameter(0)=X-PAYPAL-APPLICATION-ID
Am I missing something or this is entirely not possible?
#Krishnan, seems you are missing a HTTP header called X-PAYPAL-APPLICATION-ID.