Error "request body stream exhausted" error when sending form to .aspx from UIWebView

I upload one web form .aspxto UIWebViewan iPhone and the form loads successfully. Now the form has one photo selection button, where users can select a photo / image from the iPhone gallery and one submit button. Having selected the photo, and when I click the submit button on the form, it gives an error, for example, " request body stream exhausted". I also searched on google but no luck. Here is a snippet of code

- (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 was able to upload the image and submit the form from the Safari application on the device. Please help me. Thanks in advance.

+4
source share

Source: https://habr.com/ru/post/1533486/


All Articles