使用正常的方式通过 NSURLConnection 框架发送 http 消息不会运行,我尝试使用 CoreFoundation.Framework 来做到这一点:
-(void)implementaZioneChiamata{
for (int i = 0; i < [_arrayOfSerial count]; i++) {
CFStringRef urlFirst = CFSTR("http://192.168.30.9:88/virtualBadge.aspx?token=");
CFStringRef token =(CFStringRef)_codicePWD;
CFStringRef urlSecond = CFSTR("&serial=");
CFStringRef serial = (CFStringRef) [_arrayOfSerial objectAtIndex:i];
NSLog(@"URL: %@%@%@%@", urlFirst,token,urlSecond,serial);
CFStringRef strs[4];
strs[0] = urlFirst;
strs[1] = token;
strs[2] = urlSecond;
strs[3] = serial;
CFArrayRef urlArray = CFArrayCreate(kCFAllocatorDefault, (void*)strs, 4, &kCFTypeArrayCallBacks);
CFStringRef urlCompleta = CFStringCreateByCombiningStrings(kCFAllocatorDefault, urlArray, CFSTR(""));
NSLog(@"Url Completa: %@",urlCompleta);
CFURLRef myURL = CFURLCreateWithString(kCFAllocatorDefault, urlCompleta, NULL);
CFStringRef headerFieldName = CFSTR("host");
CFStringRef headerFieldValue = CFSTR("192.168.30.9");
CFStringRef requestMethod = CFSTR("GET");
CFHTTPMessageRef myRequest =CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, myURL,
kCFHTTPVersion1_1);
CFHTTPMessageSetHeaderFieldValue(myRequest, headerFieldName, headerFieldValue);
CFDataRef mySerializedRequest = CFHTTPMessageCopySerializedMessage(myRequest);
NSString *messReq = [[NSString alloc] initWithData:(NSData*)mySerializedRequest encoding:NSASCIIStringEncoding];
CFStream
NSLog(@"Serialized Request: %@", messReq);
}
在代码的第一部分中,我创建了 URL。在此之后,我初始化 CFHTTPMessageRef 以创建 HttpRequest。但现在出现了问题:
一旦我有 HttpMessage 发送它,我需要创建 CFStreamRef 来发送消息。从创建流的想法开始,我如何发送消息?使用 CFStream API?
好的 Guy Tnx 在打开流后寻求帮助,请求是发送添加这样的流。
CFReadStreamRef myReadStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, myRequest);
CFReadStreamOpen(myReadStream);
全部!