I'm trying to use the "CocoaHTTPServer" found at https://github.com/robbiehanson/CocoaHTTPServer. I have added it to my project, and now, if i type on my browser something like this: 192.168.3.114:45000 i receive an html page called index with a simple welcome message (this page is stored inside the default project). This is ok. It works correctly. What i need to understand now, is how can i for example do a simple GET request typing on the browser something like "192.168.3.114:52000/getElement" and receive on the browser a simple String. Can you please give me help? I don't know where i can configure or check this because there are some classes. I'm trying to study the HTTPConnection class but i'm going in confusion because i'm new on the objective-c programming. Thanks
2 Answers
You have to use a custom HTTPConnection
subclass
@interface MyHTTPConnection : HTTPConnection
...
@end
then you could do custom URL handling
@implementation MyHTTPConnection
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
{
HTTPLogTrace();
if ([path isEqualToString:@"/getElement"])
{
NSData *data = ...
HTTPDataResponse *response = [[HTTPDataResponse alloc] initWithData:data];
return response;
}
// default behavior for all other paths
return [super httpResponseForMethod:method URI:path];
}
@end
and the set HTTPServer connectionClass
so that your server knows you want to handle the connections yourself
[httpServer setConnectionClass:[MyHTTPConnection class]];
-
Thanks @eik ! The implementation code is now clear for me. I have some doubts about the interface MyHTTPConnection : HTTPConnection. Where should i have to add it? Have i to modify the HTTPConnection.h class?– HieickerAug 27, 2013 at 13:17
-
No, and an extra class in Xcode: "File > New File ...", "Objective-C class" and set "Class" to "MyHTTPConnection" (or any name you like) and "Subclass of" to "HTTPConnection". Don't forget to
#import "HTTPConnection.h"
in MyHTTPConnection.h.– eikAug 27, 2013 at 14:38
You can do an NSURL request and then get the server response as an NSString:
NSString *URL = @"http://yoururlhere.com?var1=";
URL = [URL stringByAppendingString: yourvarstring];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: URL]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
//Check if the server has any output
if([serverOutput length] == 0)
{
//Do something
} else {
//Do Something else
}
-
thanks for the help. But where you should use this kind of code in the CocoaHTTPServer? Have you see the classes?– HieickerAug 27, 2013 at 11:12
-
I think perhaps I don't fully understand what you're trying to accomplish. Do you just want to return data from the HTTP server into your application or does your question have specific connotations with regards to the services running on your HTTPServer stack?– CompyAug 27, 2013 at 11:45
-
I have the CocoaHttpServer inside the device. When i type the ip address of this device inside my browser (in my mac mini) i need to send to the browser a simple message. At the moment the server already return a simple html page, but i need to modify it and i don't know how can i do. Thanks– HieickerAug 27, 2013 at 13:09