我正在使用 ASIHTTPRequest 框架尝试通过 iPhone 将文件上传到我的网络服务器。下面是代码。只要我不使用 setFile 方法,我就会从服务器返回 200,所以一切都很好。一旦我实现了 setFile,服务器就会返回 0。我预计会出现 401 或类似的情况,因为我可以想象我正在处理身份验证问题。我的服务器是IIS,为什么我在请求中使用NTLM方式。我错过了什么吗?
NSInteger httpStatus;
NSString *httpResponseString;
NSError *httpRequestError;
NSArray *paths = [[[NSArray alloc] init] autorelease];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"abiliator_basis_de_ar.xml"];
NSURL *myURL = [NSURL URLWithString: @"http://my.url.com/"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURL];
[request setShouldPresentCredentialsBeforeChallenge:NO];
[request setUsername:@"myUserName"];
[request setPassword:@"myPassword"];
[request setDomain:@"myDomainName"];
[request setFile:[NSURL URLWithString:filename] forKey:@"xml"];
[request startSynchronous];
httpRequestError = [request error];
httpResponseString = [request responseString];
httpStatus = [request responseStatusCode];
if (!httpRequestError) {
httpStatus = [request responseStatusCode];
NSLog(@"Class %@, Method: %@ - OK login and filetransfer successful '%i'", self.myClassName, NSStringFromSelector(_cmd), httpStatus);
}
else {
NSLog(@"Class %@, Method: %@ - Error '%i' occurred sending the http request", self.myClassName, NSStringFromSelector(_cmd), httpStatus);
}
是的,文件存在,这里是 ls 的结果:
rene-stegs-macbook-pro:~ renesteg$ ls -ltr '/Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml'
-rw-r--r--@ 1 renesteg staff 1062 22 Mai 13:44 /Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml
这里是文件名的值:
Filename string is: '/Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml