我送的base64字符串PHP服务器和它运作良好。 现在我想给另一个参数为字符串。 谁能告诉我什么样的代码需要在添加下面的代码。
下面的代码工作适合单参数。 我们如何修改它的多个参数?
NSData *data = [UIImageJPEGRepresentation(imgeview.image,90) base64Encoding];
// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"question_image=%@",data];
myRequestString = [myRequestString stringByReplacingOccurrencesOfString:
@"+" withString:@"%2B"];
// Create Data from request
NSData *myRequestData = [NSData dataWithBytes:[myRequestString UTF8String]
length:[myRequestString length]];
request = [[NSMutableURLRequest alloc] initWithURL:
[NSURL URLWithString:@"http://192.168.0.101/Mobile_tutor/webservice/question_details.php"]];
// set Request Type
[request setHTTPMethod:@"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody:myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes]
length:[returnData length]
encoding:NSUTF8StringEncoding];
NSLog(@"-------------%@",response); // here you get reasponse string
对于网络操作这些就像是AFNetworking巫提供异步工作,并更好地支持API的方式更好地处理
教程AFNetworking
从这里获取
NSArray *keys = @[@"UserID", ];
NSArray *objects = @[@(userId)];
NSDictionary *parameter = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:
[NSURL URLWithString:BaseURLString]];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"services/UserService.svc/GetUserInfo"
parameters:parameter];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError* error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
// do what ever
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
给定一个NSDictionary
“PARAMS”,其键和值是字符串,其中每个条目代表一个参数(名称/值),你可以定义一个辅助类别:
@interface NSDictionary (FormURLEncoded)
-(NSData*) dataFormURLEncoded;
@end
dataFormURLEncoded
返回从在字典中给定参数的正确编码的字符序列。
编码算法是W3C指定: URL编码形式的数据/应用/ X WWW的窗体-urlencoded编码算法
它可以实现如下:
首先,一个辅助功能,其编码参数名称,分别一个参数值:
static NSString* x_www_form_urlencoded_HTML5(NSString* s)
{
// http://www.w3.org/html/wg/drafts/html/CR/forms.html#application/x-www-form-urlencoded-encoding-algorithm , Editor's Draft 24 October 2013
CFStringRef charactersToLeaveUnescaped = CFSTR(" ");
CFStringRef legalURLCharactersToBeEscaped = CFSTR("!$&'()+,/:;=?@~");
NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(__bridge CFStringRef)s,
charactersToLeaveUnescaped,
legalURLCharactersToBeEscaped,
kCFStringEncodingUTF8));
return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}
最后, dataFormURLEncoded
构成所述编码参数的字符序列。 A“参数”,将通过连接编码可以由name
, =
和编码的value
:
parameter := name "=" value
然后,参数列表将通过由一个“&”将它们分开串联参数组成:
parameters := parameter ["&" parameter]
它可以实现如下:
@implementation NSDictionary (FormURLEncoded)
-(NSData*) dataFormURLEncoded {
NSMutableData* data = [[NSMutableData alloc] init];
BOOL first = YES;
for (NSString* name in self) {
@autoreleasepool {
if (!first) {
[data appendBytes:"&" length:1];
}
NSString* value = self[name];
NSData* encodedName = [x_www_form_urlencoded_HTML5(name) dataUsingEncoding:NSUTF8StringEncoding];
NSData* encodedValue = [x_www_form_urlencoded_HTML5(value) dataUsingEncoding:NSUTF8StringEncoding];
[data appendData:encodedName];
[data appendBytes:"=" length:1];
[data appendData:encodedValue];
first = NO;
}
}
return [data copy];
}
@end
注:字符序列编码使用Unicode UTF-8的字符串。
例:
鉴于你的参数:
NSDictionary* params = @{@"a": @"a a", @"b": @"b+b", @"c": @"ü ö"};
NSData* encodedParamData = [params dataFormURLEncoded];
现在, encodedParamData
将被添加到你的身体,其内容类型为application/x-www-form-urlencoded
。
编码参数字符串变成:
A = A + a和b = B%2BB&C =%C3%BC +%C3%B6