Objective-C/CocoaHttpServer-尝试使用一个参数执行简单的GET请求 [英] Objective-C/CocoaHttpServer - Trying to do a simple GET request with one parameter
问题描述
我正在尝试使用在 https://github.com/robbiehanson/CocoaHTTPServer .我已将其添加到我的项目中,现在,如果我在浏览器中键入以下内容:192.168.3.114:45000我会收到一个名为index的html页面,带有简单的欢迎消息(此页面存储在默认项目中).还行吧.它可以正常工作.我现在需要了解的是,例如,如何执行在浏览器上键入"192.168.3.114:52000/getElement"之类的简单GET请求,并在浏览器上接收到简单的String.你能帮我个忙吗?我不知道在哪里可以配置或检查它,因为有一些类.我正在尝试研究HTTPConnection类,但由于我在Objective-C编程上是新手,所以我很困惑. 谢谢
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
推荐答案
您必须使用自定义的HTTPConnection
子类
You have to use a custom HTTPConnection
subclass
@interface MyHTTPConnection : HTTPConnection
...
@end
然后您可以进行自定义URL处理
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
和设置HTTPServer connectionClass
,以便您的服务器知道您要自己处理连接
and the set HTTPServer connectionClass
so that your server knows you want to handle the connections yourself
[httpServer setConnectionClass:[MyHTTPConnection class]];
这篇关于Objective-C/CocoaHttpServer-尝试使用一个参数执行简单的GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!