简单的基于 ASIHTTPRequest 的函数似乎在上传时挂起

发布于 2024-11-05 12:06:15 字数 1481 浏览 1 评论 0原文

我最近开始尝试为程序实现 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 身份验证的用户名/密码,以及我想要上传的文件的路径,但我对该语言和这个库的经验很少,所以我有点迷失。我无法给出任何具体的错误或原因来说明它为何挂起,我只知道在程序中单击上传后,它会运行此功能,并且程序在尝试上传文件时挂起。这个函数有什么我遗漏或做错的吗?我真的很感激你们能提供的任何帮助。

谢谢 :)

I've recently started trying to implement HTTP upload support to a program, but I've been having some difficulty doing so. This is the first time I've ever used objective-c (although I have a C background), so I'm still very new to the language. I've been trying to get it to work using the HTTPRequest library, but haven't been able to get anything to start working. It's a fairly large program (2500~ lines) so I won't paste it here. I'll just paste the function itself here.

- (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, and RespPWD are all set in another area of the program. Basically, I've got the username/PW for the HTTP authentication, and the path to the file I want to upload, but I've very little experience with the language and this library, so I'm a bit lost. I can't give any specific errors or reasons as to why it hangs, I just know that after I click upload in the program, it runs through this function, and the program hangs trying to upload the file. Is there anything I'm missing or doing wrong in this function? I'd really appreciate any help you guys could lend.

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情域 2024-11-12 12:06:15

ASIHTTPRequest 的异步网络利用了委托设计模式。将请求的 delegate 属性设置为 self,让该类遵守 ASIHTTPRequestDelegate 协议,并实现 - (void)requestDidFinish:< /code> 和 - (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");
}

ASIHTTPRequest's asynchronous networking takes advantage of the delegate design pattern. Setting the request's delegate property to self, having that class adhere to the ASIHTTPRequestDelegate protocol, and implementing - (void)requestDidFinish: and - (void)requestdidFail: should give you callbacks for finish and failure. Quick example:

- (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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文