多部分/混合在Iphone应用程序



我正在尝试在iphone应用程序中发送多部分/混合请求,但服务总是返回http状态码500。我无法访问web服务的源代码。在android中,注册服务的调用方式如下:

HttpPost httpost = new HttpPost(PlayerUrl.REGISTER_URL);
    httpost.setHeader("Content-Type", "multipart/mixed");
    MultipartEntity entity = new MultipartEntity();
    String jsonData = "{"firstName":"abc","lastName":"xyz","email":"abc@xyz.com","password":"abc","inviteType":"1"}";
    StringBody jsonBean = new StringBody(jsonData, "application/json", null);
    entity.addPart("user", jsonBean);
    entity.addPart("image", new FileBody(new File(
    "/data/face.png"), "face.png",
    "application/octet-stream", null));
    httpost.setEntity(entity);
    HttpResponse response = client.execute(httpost);
    System.out.println(response);

但是,如何在iphone中做到这一点,我不知道。我找了一整天,却一无所获。我目前有以下代码:

 request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:method];
    //build body        
    NSMutableData *bodyForRequest = [NSMutableData data];
    //NSString *jsonData = [NSString stringWithFormat:@"%@", dataForJson];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"91473780983146649988274664144";
    NSString *contentType = [NSString stringWithFormat:@"multipart/mixed; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    [request addValue:@"1.0" forHTTPHeaderField: @"MIME-Version"];
    [bodyForRequest appendData:[[NSString stringWithFormat:@"If you can see this MIME than your client doesn't accept MIME types!rn--%@rn", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [bodyForRequest appendData:[@"Content-Type: application/json;name = "user"rn" dataUsingEncoding:NSUTF8StringEncoding]];
    [bodyForRequest appendData:[@"Content-Transfer-Encoding: 7bitrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
    //[bodyForRequest appendData:[@"Content-ID: userrnrn" dataUsingEncoding:NSUTF8StringEncoding]];
    //[bodyForRequest appendData:[[NSString stringWithFormat:@"Content-Disposition: name="user"rnrn"] dataUsingEncoding:NSUTF8StringEncoding]];

    [bodyForRequest appendData:dataForJson];
    [bodyForRequest appendData:[[NSString stringWithFormat:@"rn--%@rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [bodyForRequest appendData:[@"Content-Type: image/jpg; name="image"rn" dataUsingEncoding:NSUTF8StringEncoding]];
    [bodyForRequest appendData:[@"Content-Transfer-Encoding: base64rn" dataUsingEncoding:NSUTF8StringEncoding]];
    //[bodyForRequest appendData:[@"Content-ID: imagernrn" dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *str=[NSString stringWithFormat:@"Content-Disposition: attachment ;file="image.jpg";rnrn"];
    [bodyForRequest appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
    [bodyForRequest appendData:[NSData dataWithData:dataForImage]];

    [bodyForRequest appendData:[[NSString stringWithFormat:@"--%@--rn",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [request setHTTPBody:bodyForRequest];
    // save request start time
    requestStartTime = [CNUtility currentTimeInMilliseconds];
    // _connection the request now
    NSLog(@"%@...request...%@",self,request);
    _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [_connection start];

我的要求是将JSON数据与配置文件图像一起发送到服务器,请帮助我,或者请建议我一些可以帮助我的东西。

我也不允许使用ASI,所以如果有其他的东西,请告诉我。

尝试使用AFNetworking使您的生活更轻松,您的代码将看起来像这样:与您相关的重要部分用粗体显示

AFHTTPClient *httpClient       = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", kUrl]]];

重要部分——start

NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST"
                                                                       path:[NSString stringWithFormat:@"link?access_token=%@", kAPIToken]
                                   parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
                                  {
                                      [formData appendPartWithFileData:videoData name:@"link[data]" fileName:@"filename.mov" mimeType:@"video/quicktime"];
                                      NSString  *tagStr  = @"#partytrain";
                                      NSData    *tagData = [tagStr dataUsingEncoding:NSUTF8StringEncoding];
                                      [formData appendPartWithFormData:tagData name:@"link[text]"];
                                  }];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite)
 {
     //use a progress indicator here
     NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
 }];
[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success");
    [picker dismissViewControllerAnimated:YES completion:nil];
}
                                  failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"error: %@",  operation.responseString);}];
[operation start];

最新更新