如何在 nsurlconection 中执行 post?
how to do post in nsurlconection?
http://[domain]/functionsdata.php?action=get_projectdetails
这是我的 url 如何做 post 城市以及如何根据 objective c ios 中的 nsurl 连接中的城市键获取数据?
NSString * requestUrl = @"http://[domain]/functionsdata.php?action=get_projectdetails";
NSMutableRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: requestUrl]];
[request setHTTPMethod:@"POST"];
NSDictionary *requestParams = [@"city":@"value"];
NSData *jsonRequestData = [NSJSONSerialization dataWithJSONObject:requestParams options:kNilOptions error:&error];
[request setHTTPBody:jsonRequestData];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
我已经为自己的项目编写了该代码,但它也应该适合您的问题。这是使用它所需的代码。您可以将它添加到两个单独的文件或将要使用它的同一个文件中。
@implementation NSString (VMMStringWebStructure)
-(NSString*)stringToWebStructure
{
NSString* webString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
webString = [webString stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
webString = [webString stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
webString = [webString stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
webString = [webString stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
return webString;
}
@end
@implementation NSWebUtilities
+(NSString*)launchURL:(NSURL*)url withPostString:(NSString*)post
{
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (response.statusCode >= 200 && response.statusCode < 300)
{
NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
if (responseData.length > 0) return responseData;
}
else
{
if (error) return [error localizedDescription];
return [NSString stringWithFormat:NSLocalizedString(@"Received invalid status code: %d.",nil),response.statusCode];
}
return nil;
}
+(NSString*)launchURL:(NSURL*)url withPostValues:(NSDictionary*)postDict
{
NSArray* postKeys = postDict.allKeys;
NSMutableArray* postLines = [[NSMutableArray alloc] init];
for (NSString* key in postKeys)
{
[postLines addObject:[NSString stringWithFormat:@"%@=%@",key,[postDict[key] stringToWebStructure]]];
}
return [self launchURL:url withPostString:[postLines componentsJoinedByString:@"&"]];
}
@end
在你的情况下,你可以这样使用它:
NSString* urlString = @"http://[domain]/functionsdata.php?action=get_projectdetails";
NSURL *url = [NSURL URLWithString:urlString];
NSDictionary* postValues = @{@"city",@"<city name goes here>"};
NSString* output = [NSWebUtilities launchURL:url withPostValues:postValues];
将检索输出的 NSString
称为 output
。
http://[domain]/functionsdata.php?action=get_projectdetails 这是我的 url 如何做 post 城市以及如何根据 objective c ios 中的 nsurl 连接中的城市键获取数据?
NSString * requestUrl = @"http://[domain]/functionsdata.php?action=get_projectdetails";
NSMutableRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: requestUrl]];
[request setHTTPMethod:@"POST"];
NSDictionary *requestParams = [@"city":@"value"];
NSData *jsonRequestData = [NSJSONSerialization dataWithJSONObject:requestParams options:kNilOptions error:&error];
[request setHTTPBody:jsonRequestData];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
我已经为自己的项目编写了该代码,但它也应该适合您的问题。这是使用它所需的代码。您可以将它添加到两个单独的文件或将要使用它的同一个文件中。
@implementation NSString (VMMStringWebStructure)
-(NSString*)stringToWebStructure
{
NSString* webString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
webString = [webString stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
webString = [webString stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
webString = [webString stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
webString = [webString stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"];
return webString;
}
@end
@implementation NSWebUtilities
+(NSString*)launchURL:(NSURL*)url withPostString:(NSString*)post
{
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (response.statusCode >= 200 && response.statusCode < 300)
{
NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
if (responseData.length > 0) return responseData;
}
else
{
if (error) return [error localizedDescription];
return [NSString stringWithFormat:NSLocalizedString(@"Received invalid status code: %d.",nil),response.statusCode];
}
return nil;
}
+(NSString*)launchURL:(NSURL*)url withPostValues:(NSDictionary*)postDict
{
NSArray* postKeys = postDict.allKeys;
NSMutableArray* postLines = [[NSMutableArray alloc] init];
for (NSString* key in postKeys)
{
[postLines addObject:[NSString stringWithFormat:@"%@=%@",key,[postDict[key] stringToWebStructure]]];
}
return [self launchURL:url withPostString:[postLines componentsJoinedByString:@"&"]];
}
@end
在你的情况下,你可以这样使用它:
NSString* urlString = @"http://[domain]/functionsdata.php?action=get_projectdetails";
NSURL *url = [NSURL URLWithString:urlString];
NSDictionary* postValues = @{@"city",@"<city name goes here>"};
NSString* output = [NSWebUtilities launchURL:url withPostValues:postValues];
将检索输出的 NSString
称为 output
。