我嘗試從服務器獲取數據。我使用NSURLConnectionDataDelegate,NSURLConnectionDelegate。有代碼(Objective - C)。HTTP狀態碼411 - 所需長度
-(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];
}
,但我得到 「411 - 需要的長度」 當我用
[request setHTTPBodyStream:stream];
和 「HASH(someAddress)」 當我使用
[request setHTTPBody:reqData];
我試圖
[request setHTTPBodyStream:stream];
NSString *postLength = [NSString stringWithFormat:@"%d", [reqData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
但又是「HASH(someAdddress)」
我做錯了什麼? 對不起,我的英文。謝謝:)