Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

ios - _kCFStreamErrorCodeKey=-2102 only with wifi of some ISPs

I use below code to send a file to the server:

NSString *urlString = [NSString stringWithFormat:@"%@%@",[LIUtility sharedUtility].uploadConnectionURL,BR_SERVER_UPLOAD_ADDRESS_FILE];

self.request =[[NSMutableURLRequest alloc] init];
[self.request setURL:[NSURL URLWithString:urlString]];
[self.request setHTTPMethod:@"POST"];

PKMultipartInputStream *body = [[PKMultipartInputStream alloc] init];

NSString *requestString =[self getRequestStringForRange:range andExtension:fileName];
NSData *requestData = [requestString dataUsingEncoding:NSUTF8StringEncoding];

NSString *jsonLengthString = [NSString stringWithFormat:@"%04lu",(unsigned long)requestData.length];
NSData *jsonLengthData = [jsonLengthString dataUsingEncoding:NSUTF8StringEncoding];

[body addPartWithName:@"jsonLength" data:jsonLengthData];
[body addPartWithName:@"json" data:requestData];
[body addPartWithName:@"separator" string:@"
"];
[body addPartWithName:@"fileData" filename:fileName stream:dataStream streamLength:streamSize];

[self.request setHTTPBodyStream:body];
[self.request setTimeoutInterval:60];
NSString *contentType = LI_CONNECTION_CONTENTTYPE;
[self.request addValue:contentType forHTTPHeaderField: @"Content-Type"];

self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self];
_responseData = [NSMutableData data];
[self.connection start];

The file is uploading with 3G/4G.The code also works when device is connected to wifi of some ADSL ISPs but in some other ISPs the code do not work and I get below error:

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSErrorFailingURLStringKey=http://example.net, _kCFStreamErrorCodeKey=-2102, NSErrorFailingURLKey=http:/example.net:80/au, NSLocalizedDescription=The request timed out., _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x16a40410 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSErrorFailingURLStringKey=http://example.net, NSErrorFailingURLKey=http://example.net, NSLocalizedDescription=The request timed out., _kCFStreamErrorDomainKey=4}}}

Does anyone have any idea about what is the problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I finally solved my problem by adding Content-Length to the request.

When we do not add the Content-Length header to the request, NSURLConnection send it as a chunked request and it seems such problems is common with chunked requests.

Actually I do not found the root cause of the problem with this chunked request (I searched many subjects in web, reads many articles, talked with several other programmers but do not find any root cause, so I decide to use the other solution ) but I shared this solution for someone that can use non-chunked request in their business. As I know most usages of chunked requests are in real time streaming that we do not know the Content-Length so if you can add Content-Length, I suggest to use it for avoiding chunked problems like this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...