我最近开始尝试为程序实现HTTP上传支持,但我一直遇到一些困难。这是我第一次使用objective-c(虽然我有C背景),所以我对这门语言还很新。我一直在尝试使用HTTPRequest库来使用它,但是无法开始工作。这是一个相当大的程序(2500行)所以我不会在这里粘贴它。我只是在这里粘贴函数。
- (void)Upload2HTTP2:(NSString *)ZipPath
{
[self UpdateProgressBar:@"Upload2HTTP opening Connection to Website..."];
NSLog(@"Upload2HTTP called\n");
//URL to be used to upload
NSURL *url = [NSURL URLWithString:@"http://ftp.website.com"];
NSLog(@"Upload2HTTP -%@\n",url);
//Creates the new ASIFormDataRequest object to do the uploading
//Uses the ASIHTTPRequest and ASIFormDataRequest libraries
// http://allseeing-i.com/ASIHTTPRequest/ for more information
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addRequestHeader:@"Referer" value:@"http://ftp.website.com"];
//Sets the authentication information
//This should have already been retrieved in RetrieveFromBrowser
[request setUsername:RespUID];
[request setPassword:RespPWD];
//Sets the file to be uploaded
[request setFile:ZipPath forKey:@"Customer Upload"];
//Starts the transfer?
[request startAsynchronous];
}
ZipPath,RespUID和RespPWD都设置在程序的另一个区域。基本上,我有HTTP身份验证的用户名/ PW,以及我要上传的文件的路径,但是我对语言和这个库的经验很少,所以我有点迷失。我不能给出任何具体的错误或原因,为什么它会挂起,我只知道在程序中单击上传后,它会运行此功能,程序会挂起尝试上传文件。在这个功能中有什么我遗漏或做错了吗?我真的很感激你们可以借给他们任何帮助。
谢谢:)
答案 0 :(得分:1)
ASIHTTPRequest的异步网络利用了delegate design pattern。将请求的delegate
属性设置为self
,使该类符合ASIHTTPRequestDelegate
协议,并且实施- (void)requestDidFinish:
和- (void)requestdidFail:
应该为您提供完成和失败的回调。快速举例:
- (void)Upload2HTTP2:(NSString *)ZipPath
{
...
request.delegate = self;
[request startAsynchronous];
}
- (void)requestDidFinish:(ASIHTTPRequest *)request
{
NSLog(@"Success! Do stuff here with [request responseData] or [request responseString]");
}
- (void)requestDidFail:(ASIHTTPRequest *)request
{
NSLog(@"Failure! Check out [request error] for details");
}