I try to get data from server. I use NSURLConnectionDelegate, NSURLConnectionDataDelegate. There is code (Objective - C).
-(void)sendRequest
{
NSURL* url = [[NSURL alloc] initWithString:@"http://SomeServer"];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
NSString* reqBody = [NSString stringWithFormat:@"<tag>Content</tag>"];
NSData* reqData = [reqBody dataUsingEncoding:NSUTF8StringEncoding];
NSInputStream* stream = [NSInputStream inputStreamWithData:reqData];
[request setURL:url];
[request setHTTPBodyStream:stream];
[request setHTTPMethod:@"POST"];
self.wpData = [[NSMutableData alloc] init];
NSURLConnection* conection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.wpData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
NSString* str = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
NSLog(@"RESPONSE DATA: %@",str);
[self.wpData appendData:d];
}
But I get "411 - Length Required" when I use
[request setHTTPBodyStream:stream];
and "HASH (someAddress)" when I use
[request setHTTPBody:reqData];
I tried
[request setHTTPBodyStream:stream];
NSString *postLength = [NSString stringWithFormat:@"%d", [reqData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
but again "HASH (someAdddress)"
What have I done wrong? Sorry for my English. Thanks :)
Fortunately, you can easily fix the “411 Length Required” error. This HTTP status code happens when the server requires a content-length header, but it isn't specified in a request. To resolve this issue, you can simply define a content length.
What Is a 411 Status Code? The server refuses to accept the request without a defined Content-Length1.
The Content-Length is optional in an HTTP request. For a GET or DELETE the length must be zero. For POST, if Content-Length is specified and it does not match the length of the message-line, the message is either truncated, or padded with nulls to the specified length.
Informational responses ( 100 – 199 ) Successful responses ( 200 – 299 ) Redirection messages ( 300 – 399 ) Client error responses ( 400 – 499 )
What have I done wrong?
Nothing.
HTTP Status Code 411 (Length Required) is sent by the server as a response when it refuses to accept a message without a content-length header, for whatever reason.
A server simply may or may not accept a content without a Content-Length header.
When you set an NSInputStream
object as request body via property HTTPBodyStream
for the request, NSURLConnection
cannot evaluate the length of the body itself anymore. (there is no property length
for a stream). Hence, NSURLConnection
uses a certain "transfer mode", namely "chunked transfer encoding". This transfer mode should succeed to transmit any body and it does not require a Content-Legth header (actually must not contain one). Alas, the server simply does not accept this type of transfer.
See also: Chunked transfer encoding (wiki).
To solve the issue on the client side:
Determine the length of the body yourself (if possible) and set a "Content-Length" header field for the request. If this input stream has been created from a file or from a NSData
object, the length can be easily determined. But be sure to set the exact same length as the actual stream content in bytes.
Don't use a NSInputStream
, but use a NSData
object as body and set it via property HTTPBody
. When you set the body as a NSData
object, NSURLConnection
can determine the content length itself, and it will automatically add a Content-Length header with the correct length, unless you set it yourself in the request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With