如何在 nsurlconection 中发布



>http://[domain]/functionsdata.php?action=get_projectdetails这是我的网址如何发布城市以及如何根据目标c ios中的nsurl连接中的城市键获取数据?

我已经

为我自己的项目编写了该代码,但它也应该适合您的问题。这是使用它所需的代码。您可以将其添加到两个单独的文件或将使用它的同一文件中。

@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];

这将检索输出具有一个名为outputNSString

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];

相关内容

  • 没有找到相关文章

最新更新