I am trying to login automatically into an ASP.NET website using NSMutableURLRequest. I am sending through all the post parameters I can think of, and it works well with other websites. (eg. Wordpress based). But I am unable to get it to work for ASP.net websites. Can someone please explain to me how I could go about this issue?
NSString *post = #"__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUJNjM4NDU2MzY5ZGT%2F7rz73weImm5JbYQQ4q2lRY3HUw%3D%3D&__EVENTVALIDATION=%2FwEWBQK9gI6LAwKY4eTfDQKmw8%2B%2FAQLerrfAAwKQxuCGAT0xfZqPEPwFCfa5fbrvTZXDSnbY&ctl00%24SideBarContent%24UserName=demo&ctl00%24SideBarContent%24Password=demo&ctl00%24SideBarContent%24LoginButton=Login&ctl00%24SideBarContent%24UserTimeZoneCrawler%24offset=02%3A00%2C0";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc] init];
[postRequest setURL:[NSURL URLWithString:#"http://asp.net/website/logon.aspx"]];
[postRequest setHTTPMethod:#"POST"];
[postRequest setValue:postLength forHTTPHeaderField:#"Content-Length"];
[postRequest setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[postRequest setHTTPBody:postData];
[postRequest setValue:#"loginCookie=UserName=demo; SessionContext=ydnwyyz5n50mt2zjn3yiarq0" forHTTPHeaderField:#"Cookie"];
Regards,
EZFrag
You might have to pass your username/password in with your URL.
[postRequest setURl:[NSURL URLWithString:#"http://<username>:<password>#asp.net/website/login.aspx"]];
I ended up with letting the users enter their usernames and passwords manually every time. I am still open to any suggestions though.
Regards,
EZFrag
Related
I am unable to upload an image from my device's Photos in a background session. When I call [session uploadTaskWithRequest:req fromFile:nsurl] the system immediately complains by sending
Failed to issue sandbox extension for file file:///var/mobile/Media/DCIM/103APPLE/IMG_3984.JPG, errno = 1
to the console. (A similar Stack Overflow issue is here)
However, if I create my NSURLSessionConfiguration with [NSURLSessionConfiguration defaultSessionConfiguration] (instead of [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:id], which I need) and if I construct an NSData object out of the NSURL and upload that instead of uploading straight from a file (which is required by a background session) then the upload succeeds. Btw I'm uploading files into our Rackspace Cloud account, and am able to do this successfully with a simple Postman PUT.
The problem occurs in my uploadObject method, which looks like:
-(void) uploadObject:(NSURL*)urlToBeUploadedIn
{
NSDictionary *headers = #{ #"x-auth-token": state.tokenID,
#"content-type": #"image/jpeg",
#"cache-control": #"no-cache" };
// create destination url for Rackspace cloud upload
NSString *sURL = [NSString stringWithFormat:#"%#/testing_folder/%#.jpg", state.publicURL, [state generateImageName]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:sURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:#"PUT"];
[request setAllHTTPHeaderFields:headers];
self.sessionIdentifier = [NSString stringWithFormat:#"my-background-session"];
// NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; // when I use this instead of the line below the sandbox error goes away
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:self.sessionIdentifier];
self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithRequest:request fromFile:urlToBeUploadedIn];
[uploadTask resume];
}
My call that invokes uploadObject: looks like:
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:state.arrImagesToBeUploaded options:nil];
PHAsset *phAsset = [fetchResult objectAtIndex:0]; // yes the 0th item in array is guaranteed to exist, above.
[phAsset requestContentEditingInputWithOptions:nil
completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
NSURL *imageURL = contentEditingInput.fullSizeImageURL;
[self uploadObject:imageURL];
}];
Btw I first validate the NSURL I send to uploadObject: with a call to fileExistsAtPath: so I know my reference to the file is good. Finally, my delegate calls
(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)dataIn
(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error does get invoked, although the data
do get called by the server (although the response won't parse) so, I am getting something back from the server which never correctly receives the image.
A solution is to first copy the image to be uploaded into the app's sandbox. I used:
NSError *err;
BOOL bVal = [myNSDataOb writeToURL:myDestinationURL options:0 error:&err];
and copied into my app's 'Documents' directory, but one might also use:
NSError *err;
BOOL bVal = [[NSFileManager defaultManager] copyItemAtURL:myImageURL toURL:myDestinationURL error:&err];
I have set up a ASP.NET Web service that I have tested on the server through the auto-generated service webpage.
But I can't post data to it correctly by an iOS app. **I'm getting no errors but its also not ever firing the method of the web service.
Please help..
NSURL *siteURL = [NSURL URLWithString:#"http://server/.../IOSDataWebService/Service1.asmx?op=testMethod"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:siteURL];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
NSString *postString = #"fieldToAdd=TestString";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:postString forHTTPHeaderField:#"Content-Length"];
[NSURLConnection connectionWithRequest:request delegate:self];
Try setting postString.length, instead of postString itself, as the value of the Content-Length header.
Oh, wait, you can't set just a bare integer there. Let's try wrapping it as a NSNumber instead with #(postString.length).
I am trying to use ObjectiveFlickr with the following sample API call but my code ends with an EXC_BAD_ACCESS. I made sure that my key and shared secret exist in flickr:
OFFlickrAPIContext *context = [[OFFlickrAPIContext alloc] initWithAPIKey:apiKey sharedSecret:sharedSecret];
OFFlickrAPIRequest *request = [[OFFlickrAPIRequest alloc] initWithAPIContext:context];
[request setDelegate:self];
[request callAPIMethodWithGET:#"flickr.photos.getRecent" arguments:[NSDictionary dictionaryWithObjectsAndKeys:#"1", #"per_page", nil]];
I had the same error, look here: ObjectiveFlickr & Xcode 4.5.2
Declare the context and request variables as properties instead, and it'll get rid of the EXC_BAD_ACCESS
Someone I work with created a website that he wants to use to provide a service. They run a php script when the user taps Login that checks username/password/etc
Now I want to write an app that would allow logging in and also retrieving data, as one would on the website. How can I pass the login info from the iOS device to that PHP script that is run on the website? And after I'm logged in, how can I check and retrieve the data?
Do I need to ask him for an API?
responseData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.yourwebsite.com/login.php"]];
NSString *params = [[NSString alloc] initWithFormat:#"user=myuser&pass=mypass"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
guys!
when i using asihttp in my app,i meant a post method with a nsmutabledata for body stream,but i can't get what should be,just couldn't callback the
- (void)requestFinished:(ASIHTTPRequest *)request;
or
- (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
delegate method. ASIFormDataRequest can't either
but Synchronous request is ok,just error for asynchronous.that's my request code below,how i can fit it for my app? thanks for any help!
ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setPostBody:data];
[request setTimeOutSeconds:30];
[request setRequestMethod:#"POST"];
[request startAsynchronous];
Try using the following:
[request setDidFinishSelector:#selector(requestDone:)];
[request setDidFailSelector:#selector(requestWentWrong:)];
That way you know for sure what methods the request should be calling when it's done.