开发者社区> 问答> 正文

IOS post请求? 400 报错

IOS post请求? 400 报错

ios中post的请求不知道怎么实现

比如说一个请求地址www.website.com

然后需要把(key,value)为(key1,value1),(key2,value2),(key3,value3)的值设进去

应该怎么做呢

展开
收起
爱吃鱼的程序员 2020-06-03 15:12:03 529 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    相当于是通过 POST 方法传递参数,在下面地址里有很多 iOS 的 HTTP 编程方面的文章,找一下,有的:)

    http://www.oschina.net/ios/264/ios-http


    ######
    //NSDictionary的key要和post提交的变量名相对应
    - (NSString *)post:(NSString *)postUrl withData:(NSMutableDictionary *)postData {
    	NSURL *url = [NSURL URLWithString:postUrl];
    	ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    	NSEnumerator *enumerator = [postData keyEnumerator];
    	id key;
    	while (key = [enumerator nextObject]) {
    		//检查是否要上传文件
    		if ([key isEqual:UPLOAD_FILES_DICT]) {
    			NSDictionary *uploadFiles = [postData objectForKey:key];
    			if ([uploadFiles count] > 0) {
    				for (id k in uploadFiles) {
    					NSString *fileName = [uploadFiles objectForKey:k];
    					if ([[NSFileManager defaultManager] fileExistsAtPath:fileName]) {
    						[request setFile:fileName forKey:k];
    					}
    				}
    			}
    		} else {
    			[request setPostValue:[postData objectForKey:key] forKey:key];
    		}
    	}
    	
    	//设置30s超时
    	[request setTimeOutSeconds:30];
    	[request startSynchronous];
    	int httpStatusCode = [request responseStatusCode];
    	NSString *response = @"";
    	if (httpStatusCode < 400 && httpStatusCode > 100) {
    		response = [request responseString];
    		NSLog(@"Ret:%@", response);
    	}
    	return response;
    }
    2020-06-03 16:46:40
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载