ios ASIHTTPRequest POST iPhone

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6235815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:13:02  来源:igfitidea点击:

ASIHTTPRequest POST iPhone

iphoneobjective-ciosasihttprequest

提问by Mark

Here's a portion of the html codeI'm trying to submit the textarea, much this textarea this forum uses to type in questions. It's not working, nothing gets sent and the type of response i get back is

这是我试图提交 textarea的html 代码的一部分,这个论坛用来输入问题的大部分 textarea。它不起作用,什么也没有发送,我得到的响应类型是

NSHTTPURLResponse: 0x617bb20

NSHTTPURL响应:0x617bb20

Though I managed to get it working for the login except i replaced body=%@ with user=%@&pass=%@

虽然我设法让它为登录工作,但我用 user=%@&pass=%@ 替换了 body=%@

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://forums.whirlpool.net.au%@", replyLink]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request addRequestHeader:@"Content-Type" value:@"application/xml;charset=UTF-8;"];
[request setPostValue:@"test" forKey:@"body"];
[request setDelegate:self];
[request startAsynchronous];

- (void) requestFinished:(ASIHTTPRequest *)request {
//NSString *responseString = [request responseString];
NSLog(@"Response %d : %@", request.responseStatusCode, [request responseString]);

//NSData *responseData = [request responseData];
}

- (void) requestStarted:(ASIHTTPRequest *) request {
NSLog(@"request started...");
}

- (void) requestFailed:(ASIHTTPRequest *) request {
NSError *error = [request error];
NSLog(@"%@", error);
}

Updated code using ASIHTTPRequest.

使用 ASIHTTPRequest 更新代码。

回答by Ben Scheirman

ASIHTTPRequest is the way to go here. It's difficult to understand what is exactly wrong with the code you've written (except that it looks like a synchronous request, which is a no-no).

ASIHTTPRequest 是这里的方法。很难理解您编写的代码到底有什么问题(除了它看起来像一个同步请求,这是一个禁忌)。

In ASIHTTPRequest you can do this:

在 ASIHTTPRequest 中,您可以这样做:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:someUrl];
[request setRequestMethod:@"POST"];
[request setPostValue:@"..." forKey:@"user"];
[request setPostValue:@"..." forKey:@"password"];

[request setDelegate:self];
[request startAsyncrhonous];

Then, make sure your class conforms to the ASIHTTPRequestDelegateprotocol and implement at the very least, this method:

然后,确保您的类符合ASIHTTPRequestDelegate协议并至少实现此方法:

- (void)requestFinished:(ASIHTTPRequest *)request {
   NSLog(@"Response %d ==> %@", request.responseStatusCode, [request responseString]);
}

You can also handle other methods if you choose, such as:

如果您选择,您还可以处理其他方法,例如:

- (void)requestStarted:(ASIHTTPRequest *)request;
- (void)requestFailed:(ASIHTTPRequest *)request;

You can always download the ASIHTTPRequest project from github at http://github.com/pokeb/asi-http-request.

您可以随时从 github 下载 ASIHTTPRequest 项目,网址http://github.com/pokeb/asi-http-request

The docs are located at http://allseeing-i.com/ASIHTTPRequest/and are fantastic.

文档位于http://allseeing-i.com/ASIHTTPRequest/并且非常棒

Hope this helps!

希望这可以帮助!

回答by Gami Nilesh

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "JSON.h"
#import "JSONKit.h"
NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"YOUR URL"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setRequestMethod:@"POST"];

    [request setPostValue:Email forKey:@"email"];
    [request setPostValue:Password forKey:@"password"];
    [request setDelegate:self];
    [request setUsername:@"signin"];
    [request startAsynchronous];
#pragma mark- Request Finish
- (void)requestFailed:(ASIHTTPRequest *)request;
{
    NSLog(@"**********    RequestFailed     **************%@",request.responseString);
    [SVProgressHUD dismiss];

}

-(void)requestFinished:(ASIHTTPRequest *)request
{
    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *resDict = [parser objectWithString:[request responseString] error:nil];
    if ([[request username] isEqualToString:@"signin"])
    {

    }
    [SVProgressHUD dismiss];
}

AFNetworking Files:https://drive.google.com/file/d/0B_RDiggCq5U3enh2WUdKaVlRcGM/view?usp=sharing

AFNetworking 文件:https://drive.google.com/file/d/0B_RDiggCq5U3enh2WUdKaVlRcGM/view ?usp =sharing

ASIHTTPREQUEST files:https://drive.google.com/file/d/0B_RDiggCq5U3LUttRm9WSS1KN2s/view?usp=sharing

ASIHTTPREQUEST 文件:https://drive.google.com/file/d/0B_RDiggCq5U3LUttRm9WSS1KN2s/view ?usp =sharing

回答by Mark

Just like to say I got it working :).

就像说我让它工作了:)。

For those who wants hints and are in my position, well look for hidden fields in forms or check this site http://www.echoecho.com/htmlforms07.htm

对于那些想要提示并在我的位置上的人,请在表单中查找隐藏字段或查看此站点http://www.echoecho.com/htmlforms07.htm

and this post to see what I mean

和这篇文章看看我的意思

http://www.iphonedevsdk.com/forum/148450-post14.html

http://www.iphonedevsdk.com/forum/148450-post14.html

For those who just want a more direct answer keep reading but please read the iphonedevsdk link that was a fundamental step in trying to figure the answer.

对于那些只想要更直接答案的人,请继续阅读,但请阅读 iphonedevsdk 链接,这是试图找出答案的基本步骤。

As an example one of the hidden fields that were in the html code was:

例如,html 代码中的隐藏字段之一是:

<input type="hidden" name="version" id="version" value="3">

this would translate to

这将转化为

[request setPostValue:@"3" forKey:@"version"];

"forKey"'s are the "name" field in the html and the "setPostValue" is the value in "html"

“forKey”是html中的“name”字段,“setPostValue”是“html”中的值

you can take the above thinking and apply it to simulate a button press from

您可以采用上述想法并将其应用于模拟从

<input type="submit" name="post" id="post" tabindex="56" style="width:150px;font:16px Arial;" value="Post Reply" onc...

to

[request setPostValue:@"submit" forKey:@"post"];

hopefully that helps others :).

希望能帮助其他人:)。