Re: NSURLConnection and HTTP Headers
Re: NSURLConnection and HTTP Headers
- Subject: Re: NSURLConnection and HTTP Headers
- From: <email@hidden>
- Date: Mon, 15 Jan 2007 09:29:29 +0000
Thanks very much, that's perfect. I didn't realise that it would return a different object to the documentation!
Either that, or my docs are out of date...
This has also solved the problem of not knowing the HTTP status code, which is returned from the same NSHTTPURLResponse object.
Now, the only strangeness I'm experiencing is that it sends various headers automagically, such as Connection: keep-alive. I'm setting Connection to Close using setValue:forHTTPHeaderField, but when my request reaches the server, Connection is keep-alive!
I've checked my code is actually setting the header, and any custom ones are sent to the server fine.
I've even set the value, made the request, then fetched back the request headers, and Connection says close, but the server says otherwise.
Can I not override the defaults? I understand that some headers have to be sent, like Host: for example, but I would really like to be able to override all the defaults if I want to.
Thanks again! :)
On Sun, 14 Jan 2007 19:30:39 -0700, John Pannell <email@hidden> wrote:
> Hi Drarok-
>
> You want to look at the *response* object, not the request object.
> In your code, you pass a reference to urlResponse to the
> sendSynchronousRequest: method... assuming you set that variable up
> earlier in code, you'll want to look at this object after your data
> comes back from the server.
>
> When looking for methods to interrogate the response, be sure to read
> the documentation on NSHTTPURLResponse - the allHeaderFields method
> returns a dictionary that you can look at.
>
> Hope this helps!
>
> John
>
>
> John Pannell
> Positive Spin Media
> http://www.positivespinmedia.com
>
> On Jan 14, 2007, at 6:17 PM, Drarok Ithaqua wrote:
>
>> Hi list, hope this mail finds you all well.
>>
>> I've been trying to debug some PHP code I've been working on that
>> returns custom HTTP headers, so I thought it would be nice to have
>> a tool to show the headers that are returned from various queries,
>> rather than using telnet to port 80. Possibly reinventing the
>> wheel, but I need the cocoa practise.
>>
>> I've got a semi-working app insofar as it sends a request to the
>> server, and shows me the response, but I don't seem to be able to
>> access the returned headers.
>>
>> I'm doing something like the following:
>> <code>
>> NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]
>> initWithURL:[NSURL URLWithString:[urlField stringValue]]];
>> // Setting various options and custom request headers here...
>> NSData *urlData = [NSURLConnection
>> sendSynchronousRequest:urlRequest returningResponse:&urlResponse
>> error:&urlError];
>> NSString *resultString = [NSString stringWithCString:[urlData
>> bytes] length:[urlData length]];
>> NSRunAlertPanel(@"Results", @"Results:\r\n%@\r\nLength:%d", @"OK",
>> nil, nil, resultString, [urlData length]);
>> </code>
>>
>> I have tried accessing [urlRequest allHTTPHeaderFields], but that
>> returns the request headers.
>> I expected that urlResponse would have something useful in, but
>> looking at the docs for that class, it doesn't really DO anything?
>>
>> Am I barking up the wrong tree with these classes? Would it be
>> better to roll my own code directly on top of sockets, or subclass
>> NSURLRequest in some way?
>> I'm just after some guidance, here.
>>
>> Thanks in advance for any suggestions.
>>
>> - Drarok
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>> 40positivespinmedia.com
>>
>> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden