我正在使用AFhttpClIEnt的子类.对于我所指的特定PUT请求,我使用了getPath:parameters:success:failure:.
到目前为止我找到的解决方案是手动构造路径变量以包含我想要传递的参数.当然,这并不理想,我想知道是否有其他选项不容易出错.
简而言之,有没有办法使用AFhttpClIEnt(子类)发送PUT请求,并将传递的参数作为查询字符串附加(和编码)(就像GET请求一样)?
解决方法 getPath:参数:成功:AFhttpClIEnt.m内部的失败方法调用requestWithMethod:path:parameters.在后一种方法中,将针对某些值检查http方法,以决定如何将参数附加到请求.如您所见,默认情况下,参数只应在GET,head或DELETE请求的情况下附加到URL.因为在PUT请求的情况下你需要将它们附加到URL,所以修改requestWithMethod:path:参数如下:- (NSMutableURLRequest *)requestWithMethod:(Nsstring *)method path:(Nsstring *)path parameters:(NSDictionary *)parameters { NSURL *url = [NSURL URLWithString:path relativeToURL:self.baseURL]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease]; [request sethttpMethod:method]; [request setAllhttpheaderFIElds:self.defaultheaders]; if ([method isEqualToString:@"GET"] || [method isEqualToString:@"head"]) { [request sethttpShouldUsePipelining:YES]; } if (parameters) { if ([method isEqualToString:@"GET"] || [method isEqualToString:@"head"] || [method isEqualToString:@"DELETE"] || [method isEqualToString:@"PUT"]) { url = [NSURL URLWithString:[[url absoluteString] stringByAppendingFormat:[path rangeOfString:@"?"].location == NSNotFound ? @"?%@" : @"&%@",AFqueryStringFromParametersWithEnCoding(parameters,self.stringEnCoding)]]; [request setURL:url]; } else { Nsstring *charset = (Nsstring *)CFStringConvertEnCodingToIANACharSetname(CFStringConvertnsstringencodingToEnCoding(self.stringEnCoding)); switch (self.parameterEnCoding) { case AFFormURLParameterEnCoding:; [request setValue:[Nsstring stringWithFormat:@"application/x-www-form-urlencoded; charset=%@",charset] forhttpheaderFIEld:@"Content-Type"]; [request sethttpBody:[AFqueryStringFromParametersWithEnCoding(parameters,self.stringEnCoding) dataUsingEnCoding:self.stringEnCoding]]; break; case AFJsONParameterEnCoding:; [request setValue:[Nsstring stringWithFormat:@"application/Json; charset=%@",charset] forhttpheaderFIEld:@"Content-Type"]; [request sethttpBody:[AFJsONStringFromParameters(parameters) dataUsingEnCoding:self.stringEnCoding]]; break; case AFPropertyListParameterEnCoding:; [request setValue:[Nsstring stringWithFormat:@"application/x-pList; charset=%@",charset] forhttpheaderFIEld:@"Content-Type"]; [request sethttpBody:[AFPropertyListStringFromParameters(parameters) dataUsingEnCoding:self.stringEnCoding]]; break; } } } return request;}总结
以上是内存溢出为你收集整理的ios – AFNetworking:将参数作为PUT请求的查询字符串附加全部内容,希望文章能够帮你解决ios – AFNetworking:将参数作为PUT请求的查询字符串附加所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)