ios - 服务器那边的请求的content-tpye是multipart/form-data,我要怎么发送数据去请求?用afnetworking
天蓬老师
天蓬老师 2017-04-17 12:02:15
0
2
1039

如题啊
该怎么弄?

之前用asi的时候是这样的

NSDictionary *sinaDBDictionary = [sinaDBArray objectAtIndex:0];
        NSString *accessTokenKey = [sinaDBDictionary objectForKey:@"AccessTokenKey"];
        NSString *wbUid = [sinaDBDictionary objectForKey:@"UserIDKey"];

        NSString *boundary = @"14745591349540787582088777204";


        //设置请求体中内容
        NSMutableString *bodyString = [NSMutableString string];
        [bodyString appendFormat:@"-----------------------------%@\r\n", boundary];
        [bodyString appendString:@"Content-Disposition: form-data; name=\"accessToken\"\r\n"];
        [bodyString appendString:@"\r\n"];
        [bodyString appendString:accessTokenKey];
        [bodyString appendString:@"\r\n"];
        [bodyString appendFormat:@"-----------------------------%@\r\n", boundary];
        [bodyString appendString:@"Content-Disposition: form-data; name=\"wbUid\"\r\n"];
        [bodyString appendString:@"\r\n"];
        [bodyString appendString:wbUid];
        [bodyString appendString:@"\r\n"];
        [bodyString appendFormat:@"-----------------------------%@--\r\n",boundary];

        NSMutableData *bodyData = [[NSMutableData alloc]initWithLength:0];
        NSData *bodyStringData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
        [bodyData appendData:bodyStringData];

        NSString *len = [NSString stringWithFormat:@"%d",[bodyData length]];

        NSString *s = [NSString stringWithFormat:@"multipart/form-data; boundary=---------------------------%@", boundary];


        NSString *url = @"屏蔽屏蔽屏蔽屏蔽掉";
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString: url]];
        request.defaultResponseEncoding = NSUTF8StringEncoding;
        request.useCookiePersistence = YES;
        [request setRequestMethod:@"POST"];
        [request addRequestHeader:@"Content-Type" value:s];
        [request addRequestHeader:@"Content-Length" value:len];
        [request setPostBody:bodyData];
        [request startSynchronous];

        NSError *error = request.error;
        if (!error) {
           // NSLog(@"%@",[request responseString]);
        }else{
           // NSLog(@"%@",[[request error]localizedDescription]);
        }

真的是好复杂啊

然后现在要转为afn了 该怎么弄

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全員に返信(2)
巴扎黑

自己写了个类目来设置request:

//
//  NSMutableURLRequest+FormData.m
//  Secrets
//
//  Created by ysq on 14-1-28.
//  Copyright (c) 2014年 ysq. All rights reserved.
//

#import "NSMutableURLRequest+FormData.h"

@implementation NSMutableURLRequest (FormData)



+(NSString *)boundaryData{
    return @"12436041281943726692693274280";
}
-(void)setFormData:(NSDictionary *)formData{

    NSString *boundary = [NSMutableURLRequest boundaryData];

    //设置请求体中内容
    NSMutableString *bodyString = [[NSMutableString alloc]init];


    for (int i=[[formData allKeys]count]-1; i>=0; i--) {

        NSString *key = [formData allKeys][i];
        NSString *value = [formData allValues][i];
        if ([key isEqualToString:@"accessToken"]) {
            value = [value substringToIndex:32];
        }

        [bodyString appendFormat:@"-----------------------------%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",boundary,key,value];
    }

    [bodyString appendFormat:@"-----------------------------%@--\r\n", boundary];


    NSMutableData *bodyData = [[NSMutableData alloc]initWithLength:0];
    NSData *bodyStringData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
    [bodyData appendData:bodyStringData];

    NSString *contentLength = [NSString stringWithFormat:@"%d",[bodyData length]];

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



    [self setValue:contentType forHTTPHeaderField:@"Content-Type"];
    [self setValue:contentLength forHTTPHeaderField:@"Content-Length"];
    [self setHTTPBody:bodyData];
    [self setHTTPMethod:@"POST"];

}



@end
いいねを押す +0
大家讲道理

AF 提供了form 请求的api

- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
                                              URLString:(NSString*)URLString
                                             parameters:(NSDictionary*)parameters
                              constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!