ios HTTP 状态代码 411 - 需要长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19227142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP Status Code 411 - Length Required
提问by WantToKnow
I try to get data from server. I use NSURLConnectionDelegate, NSURLConnectionDataDelegate. There is code (Objective - C).
我尝试从服务器获取数据。我使用 NSURLConnectionDelegate、NSURLConnectionDataDelegate。有代码(目标-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
但是当我使用时我得到“411 - Length Required”
[request setHTTPBodyStream:stream];
and "HASH (someAddress)" when I use
和“HASH (someAddress)”当我使用
[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 :)
我做错了什么?对不起我的英语不好。谢谢 :)
回答by CouchDeveloper
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.
HTTP 状态代码 411(需要长度)由服务器发送作为响应,当它拒绝接受没有内容长度标头的消息时,无论出于何种原因。
A server simply may or may not accept a content without a Content-Length header.
服务器可能会也可能不会接受没有 Content-Length 标头的内容。
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 shouldsucceed to transmit any body and it does not require a Content-Legth header (actually mustnot contain one). Alas, the server simply does not accept this type of transfer.
当您NSInputStream
通过请求的属性将对象设置为请求正文时HTTPBodyStream
,NSURLConnection
无法再评估正文本身的长度。(length
流没有属性)。因此,NSURLConnection
使用了某种“传输模式”,即“分块传输编码”。这种传递方式应该成功传输任何机构,它不需要内容Legth头(其实一定不能包含一个)。唉,服务器根本不接受这种类型的传输。
See also: Chunked transfer encoding(wiki).
另请参阅:分块传输编码(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 aNSData
object as body and set it via propertyHTTPBody
. When you set the body as aNSData
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.
自己确定正文的长度(如果可能)并为请求设置“Content-Length”标头字段。如果此输入流是从文件或
NSData
对象创建的,则可以轻松确定其长度。但一定要设置与实际流内容完全相同的长度(以字节为单位)。不要使用 a
NSInputStream
,而是使用一个NSData
对象作为主体并通过 property 设置它HTTPBody
。当你将body设置为NSData
对象时,NSURLConnection
可以自己确定内容长度,它会自动添加一个长度正确的Content-Length头,除非你自己在请求中设置。