如何模拟HTTP表单提交的iOS-objective-c
1个回答
展开全部
我AFNetworking,需要提交表单(POST)向服务器发出请求。下面是服务器所期望的一个示例:<form id="form1" method="post" action=" CodeGo.net
<input type="hidden" name="paperid" value="6">
<input type="radio" name="q77" value="1">
<input type="radio" name="q77" value="2">
<input type="text" name="q80">
</form>
我的multipartFormRequestWithMethod在AFHTTPClient作用,就像是在发送后的一个以上的图像AFNetworking。但我不知道如何与“广播”式的输入值追加表单数据。
本文地址 :CodeGo.net/509228/
-------------------------------------------------------------------------------------------------------------------------
1. 下面是一个NSURLConnection的发送POST// Note that the URL is the "action" URL parameter from the form.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL @" CodeGo.net
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x- CodeGo.net forHTTPHeaderField:@"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send
NSString *postString = @"paperid=6&q77=2&q80=blah";
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%u", [data length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection connectionWithRequest:request delegate:self];
2. 如果你看看什么样的浏览器提交到服务器这种形式(使用调试面板在浏览器中),你会看到你的POST请求的数据看起来像这样:paperid=6&q77=2&q80=blah
这是从选定的单选按钮,作为相应的POST项的值的值项,你会得到所有的单选按钮只有一个条目。 (相对于切换按钮,你在哪里得到的每个当前选定的值。) 一旦你理解了POST字符串的格式,你应该能够在ASIFormDataRequest创建请求。
3. 下面是如何做STHTTPRequestSTHTTPRequest *r = [STHTTPRequest requestWithURLString:@" CodeGo.net
r.POSTDictionary = @{ @"paperid":@"6", @"q77":"1", @"q80":@"hello" };
r.completionBlock = ^(NSDictionary *headers, NSString *body) {
// ...
};
r.errorBlock = ^(NSError *error) {
// ...
};
[r startAsynchronous];
<input type="hidden" name="paperid" value="6">
<input type="radio" name="q77" value="1">
<input type="radio" name="q77" value="2">
<input type="text" name="q80">
</form>
我的multipartFormRequestWithMethod在AFHTTPClient作用,就像是在发送后的一个以上的图像AFNetworking。但我不知道如何与“广播”式的输入值追加表单数据。
本文地址 :CodeGo.net/509228/
-------------------------------------------------------------------------------------------------------------------------
1. 下面是一个NSURLConnection的发送POST// Note that the URL is the "action" URL parameter from the form.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL @" CodeGo.net
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x- CodeGo.net forHTTPHeaderField:@"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send
NSString *postString = @"paperid=6&q77=2&q80=blah";
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:@"%u", [data length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection connectionWithRequest:request delegate:self];
2. 如果你看看什么样的浏览器提交到服务器这种形式(使用调试面板在浏览器中),你会看到你的POST请求的数据看起来像这样:paperid=6&q77=2&q80=blah
这是从选定的单选按钮,作为相应的POST项的值的值项,你会得到所有的单选按钮只有一个条目。 (相对于切换按钮,你在哪里得到的每个当前选定的值。) 一旦你理解了POST字符串的格式,你应该能够在ASIFormDataRequest创建请求。
3. 下面是如何做STHTTPRequestSTHTTPRequest *r = [STHTTPRequest requestWithURLString:@" CodeGo.net
r.POSTDictionary = @{ @"paperid":@"6", @"q77":"1", @"q80":@"hello" };
r.completionBlock = ^(NSDictionary *headers, NSString *body) {
// ...
};
r.errorBlock = ^(NSError *error) {
// ...
};
[r startAsynchronous];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询