By: Jonathan Zdziarski in iPhone Tutorials on 2010年09月06日 [フレーム]
You can use the CFHTTP API to create an HTTP request. This allows you to easily invoke HTTP GET, HEAD, PUT, POST, and most other standard requests. Creating a request involves the three-step process of creating the request object, defining the HTTP request message and headers, and serializing the message into raw protocol. Only HTTP POST requests generally contain a message body, which can contain POST form data to send. All other requests use an empty body while embedding the request parameters into the headers.In the example below, an HTTP/1.1 GET request is created, specifying the URL http://www.oreilly.com and setting the Connection header to instruct the remote end to close the connection after sending data:
CFStringRef requestHeader = CFSTR("Connection");
CFStringRef requestHeaderValue = CFSTR("close");
CFStringRef requestBody = CFSTR("");
CFStringRef url = CFSTR("http://www.oreilly.com">http://www.oreilly.com");
CFStringRef requestMethod = CFSTR("GET");
CFURLRef requestURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL);
CFHTTPMessageRef request = CFHTTPMessageCreateRequest(kCFAllocatorDefault,
requestMethod, requestURL, kCFHTTPVersion1_1);
CFHTTPMessageSetBody(request, requestBody);
CFHTTPMessageSetHeaderFieldValue(request, requestHeader, requestHeaderValue);
CFDataRef serializedRequest = CFHTTPMessageCopySerializedMessage(request);
The resulting pointer to a CFData structure provides the raw HTTP protocol output, which you would then send through a write stream to the destination server. In the example below, an HTTP GET request is created and opened through a read stream. As data flows in, the read stream's callbacks would normally be invoked to receive the new data:
int makeRequest(const char *requestURL)
{
CFReadStream readStream;
CFHTTPMessageRef request;
CFStreamClientContext CTX = { 0, NULL, NULL, NULL, NULL };
NSString* requestURLString = [ [ NSString alloc ] initWithCString:
requestURL ];
NSURL url = [ NSURL URLWithString: requestURLString ];
CFStringRef requestMessage = CFSTR("");
request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"),
(CFURLRef) url, kCFHTTPVersion1_1);
if (!request) {
return -1;
}
CFHTTPMessageSetBody(request, (CFDataRef) requestMessage);
readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request);
CFRelease(request);
if (!readStream) {
return -1;
}
if (!CFReadStreamSetClient(readStream, kCFStreamEventOpenCompleted |
kCFStreamEventHasBytesAvailable |
kCFStreamEventEndEncountered |
kCFStreamEventErrorOccurred,
ReadCallBack, &CTX))
{
CFRelease(readStream);
return -1; }
/* Add to the run loop */
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(),
kCFRunLoopCommonModes);
if (!CFReadStreamOpen(readStream)) {
CFReadStreamSetClient(readStream, 0, NULL, NULL);
CFReadStreamUnscheduleFromRunLoop(readStream,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes);
CFRelease(readStream);
return -1;
}
return 0;
}
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in iPhone )
Directory Structure for iPhone Applications
Making an HTTP Connection in iPhone Application
Making an FTP Connection in iPhone Application
Multitasking in iPhone 4 - iOS 4
SOLVED: tensorflow/contrib/lite/tools/mutable_op_resolver.h can not be found
Latest Articles (in iPhone)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate