Professional Web Applications Themes

How to do HTTP GET? - Mac Programming

Here is my code. When I run this program, I see nothing in the server access or error logs (Apache 1.3) that indicates I attempted to connect. I can put the same url in a web browser and it behaves a expected. I used a couple of Apple's Project Builder examples as a model but, I must be doing something wrong, but can't figure it out. MY callback never even gets called. Can someone help me out? TIA #import <Cocoa/Cocoa.h> #import <CoreFoundation/CoreFoundation.h> #import <CoreServices/CoreServices.h> static const CFOptionFlags kNetworkEvents = kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred; static void ReadStreamClientCallBack(CFReadStreamRef stream, ...

  1. #1

    Default How to do HTTP GET?

    Here is my code. When I run this program, I see nothing in the server
    access or error logs (Apache 1.3) that indicates I attempted to connect.
    I can put the same url in a web browser and it behaves a expected. I
    used a couple of Apple's Project Builder examples as a model but, I must
    be doing something wrong, but can't figure it out. MY callback never
    even gets called. Can someone help me out?
    TIA

    #import <Cocoa/Cocoa.h>
    #import <CoreFoundation/CoreFoundation.h>
    #import <CoreServices/CoreServices.h>

    static const CFOptionFlags kNetworkEvents = kCFStreamEventOpenCompleted |

    kCFStreamEventHasBytesAvailable |
    kCFStreamEventEndEncountered
    |
    kCFStreamEventErrorOccurred;

    static void
    ReadStreamClientCallBack(CFReadStreamRef stream, CFStreamEventType type,
    void *clientCallBackInfo);

    int main(int argc, const char *argv[])
    {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    CFReadStreamRef _stream;
    NSURL* url;
    NSProcessInfo* pinfo = [NSProcessInfo processInfo];
    NSString* hostString = [NSString stringWithString:[pinfo hostName]];
    NSScanner* scanner = [NSScanner scannerWithString:hostString];
    NSString* finalHostString;
    BOOL worked = NO;

    worked = [scanner scanUpToCharactersFromSet:[NSCharacterSet
    characterSetWithCharactersInString:"."] intoString:&finalHostString];

    CFHTTPMessageRef request;
    CFStreamClientContext ctxt = {0, (void*)NULL, NULL, NULL, NULL};
    NSString* url_string = [NSString
    stringWithString:"http://www.myserver.com/cgi-bin/user.pl?username=loren
    zo&machine=sickle&type=IN"];

    url = [NSURL URLWithString:url_string];

    request = CFHTTPMessageCreateRequest(kCFAllocatorDefault,
    CFSTR("GET"), (CFURLRef)url, kCFHTTPVersion1_1);

    _stream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefau lt,
    request);

    CFRelease(request);
    if (!CFReadStreamSetClient(_stream, kNetworkEvents,
    ReadStreamClientCallBack, &ctxt))
    return -1;


    CFReadStreamScheduleWithRunLoop(_stream,,CFRunLoop GetCurrent ()
    kCFRunLoopCommonModes);
    CFReadStreamOpen(_stream);

    CFRelease(_stream);
    [pool release];
    return 0;

    }

    static void ReadStreamClientCallBack(CFReadStreamRef stream,
    CFStreamEventType type, void *clientCallBackInfo){

    UInt8 buffer[2048];
    CFIndex bytesRead = CFReadStreamRead(stream, buffer, sizeof(buffer));

    // Dispatch the stream events.
    switch (type) {
    case kCFStreamEventHasBytesAvailable:
    if(bytesRead > 0)
    NSLog("Login results:\n%", [NSString
    stringWithCString:buffer]);
    else
    NSLog("No bytes returned");
    break;

    case kCFStreamEventEndEncountered:
    return;
    break;

    case kCFStreamEventErrorOccurred:
    return;
    break;

    default:
    break;
    }
    }
    Lorenzo Guest

  2. #2

    Default Re: How to do HTTP GET?

    In article
    <internal.thethurmans.com>,
    Lorenzo Thurman <om> wrote:
     

    URLSimpleDownload() in <URLAccess.h> works fine for me.
    David Guest

  3. #3

    Default Re: How to do HTTP GET?

    On Mon, 17 Nov 2003, Lorenzo Thurman wrote:
     

    Should you really be releasing your stream here?

    Fred

    Frederick Guest

  4. #4

    Default Re: How to do HTTP GET?

    In article
    <internal.thethurmans.com>,
    Lorenzo Thurman <om> wrote:
     

    I'm not exactly sure why you're bothering with all the Core stuff when
    you're already using Cocoa for NSURL. You should just call
    -resourceDataUsingCache: on it directly or use various class' method
    ....WithContentsOfURL: if you're dealing with something more specific
    than raw data.
    Doc Guest

  5. #5

    Default Re: How to do HTTP GET?

    In article
    <srcf.societies.cam.ac.uk>
    ,
    Frederick Cheung <DUH.ucam.org> wrote:
     
    >
    > Should you really be releasing your stream here?
    >
    > Fred
    >[/ref]

    No, probably not. My callback might be best suited for that, but since
    nothing was working...
    Lorenzo Guest

  6. #6

    Default Re: How to do HTTP GET?

    On Wed, 19 Nov 2003, Lorenzo Thurman wrote:
     
    > >
    > > Should you really be releasing your stream here?
    > >
    > > Fred
    > >[/ref]
    >
    > No, probably not. My callback might be best suited for that, but since
    > nothing was working...
    >[/ref]

    What is the retain count of your stream before you release it? If you
    destroy your stream then of course your callback won't get called.


    Fred

    Frederick Guest

  7. #7

    Default Re: How to do HTTP GET?

    In article <internal.thethurmans.com>,
    Lorenzo Thurman <om> wrote:
     

    Since you're alreay using Cocoa, could you use
    NSString's 'StringWithContentsOfURL:' to load the page instead ?


    Simon Guest

Similar Threads

  1. Forwarding HTTP POST: how to return correct HTTP status?
    By google@phildavidson.com in forum ASP.NET Web Services
    Replies: 1
    Last Post: October 14th, 03:27 PM
  2. Replies: 1
    Last Post: September 2nd, 09:53 PM
  3. #19113 [Com]: HTTP status 200 returned on HTTP CONNECT when mod_proxy not in use
    By arafuse at bcexplorers dot com in forum PHP Development
    Replies: 5
    Last Post: December 3rd, 09:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139