4

I am loading one .aspx web form on UIWebView in iPhone and the form is loading successfully. Now the form is having one choose photo button where users can select a photo/image from iPhone gallery and one submit button. After choosing a photo and when I click on submit button on the form it's throwing an error like "request body stream exhausted". I searched in google also but no luck. Here is the code snippet

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

[activity startAnimating];
activity.hidden = NO;

NSURL *url = [NSURL URLWithString:@"http://rest..com:95134/MobileForm.aspx?processName=NewForm"];

theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];//image/jpeg
[theRequest setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];// "Content-Type" = "text/html; charset=utf-8";

[webView loadRequest:theRequest];

}

#pragma mark
#pragma mark connection Methods

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"got auth challange");

    if ([challenge previousFailureCount] == 0) {
        _authed = YES;
        /* SET YOUR credentials, i'm just hard coding them in, tweak as necessary */


        [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"user" password:@"pass" persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];
    } else {

        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
    NSLog(@"received response via nsurlconnection %@",response);

    /** THIS IS WHERE YOU SET MAKE THE NEW REQUEST TO UIWebView, which will use the new saved auth info **/
    [connection cancel];

    [webView loadRequest:theRequest];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    activity.hidden =YES;
    NSLog(@"err %@", [error localizedDescription]);
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Message" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
    return YES;
}

#pragma mark
#pragma mark webView Methods

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request  navigationType:(UIWebViewNavigationType)navigationType;
{
    NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authed);

    if (!_authed) {
       _authed = NO;
       /* pretty sure i'm leaking here, leave me alone... i just happen to leak sometimes */
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];
        return NO;
    }

    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{

    activity.hidden = NO;

}
- (void)webViewDidFinishLoad:(UIWebView *)webView{

    activity.hidden = YES;

}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    activity.hidden = YES;

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
 }

But I could able to upload an image and submit the form from Safari app on device. Please help me. Thanks in advance.

Ganesh G
  • 1,991
  • 1
  • 24
  • 37
  • You should look at stackoverflow posts http://stackoverflow.com/questions/12694310/ios-6-iphone-ipad-image-upload-request-body-stream-exhausted-with-ntlm-windo and http://stackoverflow.com/questions/4359531/nsmutableurlrequest-and-request-body-stream-exhausted-error – JonBrave Nov 18 '15 at 14:19

0 Answers0