0

有人可以建议是否可以修改 EADemo 以发送 HTTP 请求(自定义标头和正文)。在我正在进行的项目中,外部附件将使用 http 请求接收数据。

在 EADemo 上书写的方法是

- (void)writeData:(NSData *)data;
4

1 回答 1

0

Ok, so I found an example which I will be using but instead of GET I will use POST. Below is the example of GET request:

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
NSLog(@"stream:handleEvent: is invoked...");

switch(eventCode) {
    case NSStreamEventHasSpaceAvailable:
    {
        if (stream == oStream) {
            NSString * str = [NSString stringWithFormat:
                @"GET / HTTP/1.0\r\n\r\n"];
            const uint8_t * rawstring =
                (const uint8_t *)[str UTF8String];
            [oStream write:rawstring maxLength:strlen(rawstring)];
            [oStream close];
        }
        break;
    }
    // continued ...
    }
}

https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/Streams/Articles/NetworkStreams.html

于 2013-11-12T20:28:41.393 回答