1

This is the curl command i use to upload PDF's and it works perfectly:

curl -u USERNAME:PASSWORD -T MyPDF.pdf —url https://mywebserver.com

I am using NSURLSession and upload task with delegates but my PDF is never uploaded.

Here is my code:

- (IBAction)upload:(id)sender {
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"https://mywebserver.com/"]];
    [request setHTTPMethod:@"POST"];

    NSString* pdfPath = [[NSBundle mainBundle] pathForResource:@"bill" ofType:@"pdf"];
    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:pdfPath]];

    [uploadTask resume];
}

- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {

}

- (void)URLSession:(NSURLSession *)session task:(nonnull NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error {
    if (error) {
        NSLog(@"Error = %@", error);
    }
}

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession]);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
    completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession *)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveData:(nonnull NSData *)data {
    if (data) {
        NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    }

}

- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(nullable NSError *)error {
    if (error) {
        NSLog(@"Error = %@", error);
    }
}
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
  • Are all of the delegates called? – Siriss May 06 '16 at 03:07
  • Yes. didReceiveData: method returns me some javascript code and looks like code for login window. But curl command works and i am not sure what is missing from my code to use credentials. – AceOfTheBase May 06 '16 at 03:09
  • 1
    I don't have anything specific to suggest as to how `curl` might actually be handling the credentials (the man page is short on specifics), but I **would** suggest you use a tool such as Charles or WireShark to observe the packets being sent from your machine to your server with the `curl` command that's working for you; I suspect there are either multiple round-trips happening to handle e.g. Kerberos authentication, or if by some magic it's all being handled in a single round-trip, it's probably buried in the headers. – fullofsquirrels May 06 '16 at 03:22

2 Answers2

0

what happens if you try to use completion block with NSURLSessionAuthChallengeUseCredential

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(nonnull NSURLAuthenticationChallenge *)challenge completionHandler:(nonnull void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {
    completionHandler(NSURLSessionAuthChallengeUseCredential, 
    [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession]);
}

in general make sure your upload works for unprotected server first. than test with credentials.

ha100
  • 1,563
  • 1
  • 21
  • 28
0

NSURLSessionAuthChallengeUseCredentialHas the same behavior.

user598789
  • 329
  • 1
  • 2
  • 17