NSURLConnection sendAsynchronousRequest重新调整媒体类型' multipart / form-data'此资源不支持

时间:2015-10-15 14:03:45

标签: ios objective-c

我正在尝试使用multipart / form-data发送发送图像和文本,但总是收到错误代码415(不支持媒体类型)。我想发送7个参数和一个图像。谁能帮我吗 ??如果我在phonegap中发送图像,那么它的工作正常,但在iOS中收到错误。 这是我的代码

// set Content-Type in HTTP header

NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=@"dfgsdgrtredsvcv6546regfdgfy54tregffd"];

// post body
NSMutableData *body = [NSMutableData data];
//-- Convert string into URL
NSString *urlString = [NSString stringWithFormat:Server Url"];
NSMutableURLRequest *request =[[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];

[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

//-- Append data into post url using following method
//-- For Sending text

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",@"amount"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[textFieldAmount.text dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"amount is %@", textFieldAmount.text);


// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n", @"asset.JPG"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", @"asset.JPG"] dataUsingEncoding:NSUTF8StringEncoding]];

// add image data
if (imgData) {
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=asset.JPG\r\n", @"43543tgregfg4654654tre"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type:image\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:imgData];
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// setting the body of the post to the reqeust
[request setHTTPBody:body];

// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];


[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (error)
    {
        NSLog(@"ERROR CONNECTING DATA FROM SERVER: %@", error.localizedDescription);
    } else
    {
    if(data.length > 0)
    {
        //success
        [self killHUD];
        NSLog(@"response is %@", response);
               }
    }`enter code here`
}];

0 个答案:

没有答案