HTTP状态码411 - 需要长度 [英] HTTP Status Code 411 - Length Required

查看:422
本文介绍了HTTP状态码411 - 需要长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从服务器获取数据。我使用NSURLConnectionDelegate,NSURLConnectionDataDelegate。有代码(目标 - C)。

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];
}

但是当我使用

[request setHTTPBodyStream:stream];

和HASH(someAddress)当我使用

and "HASH (someAddress)" when I use

[request setHTTPBody:reqData];

我试过

[request setHTTPBodyStream:stream];
NSString *postLength = [NSString stringWithFormat:@"%d", [reqData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

但又是HASH(someAdddress)

but again "HASH (someAdddress)"

我做错了什么?
对不起我的英文。谢谢:))

What have I done wrong? Sorry for my English. Thanks :)

推荐答案


我做错了什么?

What have I done wrong?

没什么。

发送HTTP状态代码411(需要长度)无论出于何种原因,当服务器拒绝接受没有内容长度标题的消息时,服务器作为响应。

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.

服务器可能接受或不接受内容一个Content-Length标题。

A server simply may or may not accept a content without a Content-Length header.

当您通过属性 NSInputStream 对象设置为请求体时> HTTPBodyStream 对于请求, NSURLConnection 无法再评估正文本身的长度。 (流没有属性 length )。因此, NSURLConnection 使用某种传输模式,即分块传输编码。此传输模式应该成功传输任何正文,并且它不需要Content-Legth标头(实际上必须不包含一个)。唉,服务器根本不接受这种类型的转移。

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.

参见:分块传输编码(wiki)。

要解决客户端问题:


  • 自己确定身体的长度(如果可能)并为请求设置Content-Length标头字段。如果已从文件或 NSData 对象创建此输入流,则可以轻松确定长度。但请确保设置与实际流内容完全相同的长度(以字节为单位)。

  • 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.

不要使用 NSInputStream ,但使用 NSData 对象作为正文并通过属性 HTTPBody 进行设置。当您将主体设置为 NSData 对象时, NSURLConnection 可以自行确定内容长度,并自动添加除非您在请求中自行设置,否则Content-Length标头的长度正确。

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.

这篇关于HTTP状态码411 - 需要长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆