开发者

ios 4.3 uploading file to wcf json web service

开发者 https://www.devze.com 2023-04-08 19:59 出处:网络
I\'ve been killing myself over this bug/problem for a few days now, and I am going insane.I have no idea why it is break.I was wondering if you guys and gals can lend me a hand and rid of my insanity.

I've been killing myself over this bug/problem for a few days now, and I am going insane. I have no idea why it is break. I was wondering if you guys and gals can lend me a hand and rid of my insanity. So thank you in advance.

What I am trying to do: upload a .wav file to a wcf json web service (.net 4.0) from an iphone app (ios 4.3). I have verified that the service does work from a different client.

The problem: the problem is the same code on the iphone app worked 5 days ago. Yesterday and today the code decided to not work. Nothing had changed on the service side and the iphone app.

I'll post as little code as I can to keep things relevant and simple to the topic. If there is a need to post more code to make it easier for you all, I will.

I am consistently getting status code 400 back from the response in the didReceiveResponse method. I've check the url which i post to many times, and the u开发者_如何学Pythonrl seem valid to me.

The size of the file which I am posting to the json web service is 1KB < fileSize < 450KB.

here is a sample url that i post to: http://random-ec2-id-east-1.elb.amazonaws.com/sampleApp/?key=ee404d54-ea45-421a-9633-1ea35c0c211e&token=zJSRqiZgmU6nOW44CeAzhWYxasdD0158yysNDCiASMk.eyJpdiI6IlU3Y2UwbWNXVGN6WVVBLU42SDVieGcifQ.kLbcRPOJ_QnrrtsBe-zF2-2IIbAffArvqeyAmwp_OpOWAoADMugHYjTPcnjkjQvzxEIMcm2k3933i3GqF2YFhAFDtItwvqre5fIGlixbuwsYhrVCm9FBoue4dCQ_pPX-yjUtq_898FGWa5INl0RG0A&type=c&platform=i

    // ################################
    - (id)init  {
      self = [super init];

      if (self) {

        self.sampleAppInstance = [sampleAppInstance sharedSampleAppInstance];

        self.sampleAppUrl = @"http://random-ec2-id.us-east-1.elb.amazonaws.com/sampleApp/";
        self.key = @"ee404d54-ea45-421a-9633-1ea35c0c211e";
        self.token = self.sampleAppInstance.facebook.accessToken; // facebook access token
        self.type = @"c";
        self.platform = @"i";

      }

      return self;
    }


    // #################################
    - (BOOL) save:(NSString *)_fileName {

      NSString *url = [NSString stringWithFormat:@"%@?key=%@&token=%@&type=%@&platform=%@", self.sampleAppUrl, self.key, self.token, self.type, self.platform];
      NSData *voiceData = [NSData dataWithContentsOfFile:_fileName];

      //NSLog(@"%@", url);

      NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]] autorelease];
      [request setValue:@"text/plain" forHTTPHeaderField:@"content-type"];
      [request setHTTPMethod:@"POST"];

      [request setValue:[NSString stringWithFormat:@"%d", [voiceData length]] forHTTPHeaderField:@"content-length"];
      [request setHTTPBody:voiceData];

      [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

      return FALSE;
    }

    // #################################
    - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
      //NSLog(@"did failed");
      [self.delegate responseDidFailWithError:error];
    }

    // #################################
    - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
      //NSLog(@"did receive data");
      //NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
      [self.delegate responseDidReceive:data];
    }

    // #################################
    - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  {
      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
      int statusCode = [httpResponse statusCode];

      // if get 400, then malform syntax
      //NSLog(@"%d", statusCode);
      if (statusCode != 200)
        [self.delegate responseDidFailWithStatusCode:statusCode];
    }


it turns out that my service only accept file size < 65k by default. the fix was to configure my service to accept file > 65k.

0

精彩评论

暂无评论...
验证码 换一张
取 消