Ios httphttp post请求参数传参能传字符串吗,求指点

3125人阅读
webview链接 get和post请求传值给链接的页面
url = [[ NSURL alloc] initWithString:@"http://www.********.:8081/index.php/Account/iphoneTopup?email=&password=1&amount=12"];
loadRequest:[ NSURLRequest requestWithURL: url ]]; //习惯采用loadRequest方式,你可采用其他方式
NSURL *url = [NSURL URLWithString: @"http://www.*******.:8081/index.php/Account/iphoneTopup"];
NSString *body = [NSString stringWithFormat: @"email=%@&password=%@&amount=%@",@"",@"1",@"12"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[WebView loadRequest: request];
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:15064次
排名:千里之外
原创:17篇
(3)(3)(4)(1)(1)(3)(3)(3)iOS(141)
在ios post请求时发json格式就不再细说,普通方式即可
发送字符串则需要特殊处理,代码如下:1,注意content-Type,2 ,对字符编码, 3,编码完成之后,前面需要加上=
(适用于C#的webapi请求)
[request setValue:@&application/x-www-form-urlencoded&
forHTTPHeaderField:@&Content-Type&];
&& & & & & &
& & & & & & NSString *baseStr = (NSString *)
& & & & & & NSString *baseString = (__bridge
NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & (CFStringRef)baseStr,
&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
CFSTR(&:/?#[]@!$&’()*+,;=&),
&& & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &
kCFStringEncodingUTF8);
& & & & & & /* 单独post字符串时,前面需要加上= */
& & & & & & NSString *postString = [NSString
stringWithFormat:@&=%@&,baseString];
& & & & & & [request setHTTPBody:[postString
dataUsingEncoding:NSUTF8StringEncoding]];
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1194433次
积分:11234
积分:11234
排名:第1369名
原创:193篇
转载:232篇
评论:106条
(4)(2)(3)(5)(2)(1)(1)(1)(2)(2)(3)(4)(1)(1)(9)(4)(1)(3)(2)(1)(2)(3)(5)(4)(3)(2)(1)(5)(5)(8)(3)(3)(6)(5)(3)(2)(13)(3)(4)(5)(3)(1)(3)(1)(1)(8)(3)(3)(1)(3)(35)(60)(76)(9)(16)(8)(2)(6)(11)(3)(5)(6)(1)(2)(3)(8)(1)(3)(6)1、使用SharedApplication,定义一个变量来传递.
2、使用文件,或者NSUserdefault来传递
3、通过一个单例的class来传递
4、通过Delegate来传递。
IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容。在IOS开发里两个UIView窗口之间传递参数方法有很多,比如
前面3种方法,暂且不说,这次主要学习如何使用通过Delegate的方法来在不同的UIView里传递数据
比如: 在窗口1中打开窗口2,然后在窗口2中填入一个数字,这个数字又回传给窗口1。
窗口2的结果传递给窗口1
1、首先定义个一委托UIViewPassValueDelegate用来传递值
@protocol UIViewPassValueDelegate
- (void)passValue:(NSString *)
这个protocol 就是用来传递值
2、在窗口1的头文件里,声明delegate
#import &UIKit/UIKit.h&
#import "UIViewPassValueDelegate.h"
@interface DelegateSampleViewController : UIViewController &UIViewPassValueDelegate&
UITextField *_
@property(nonatomic, retain) IBOutlet UITextField *
- (IBAction)buttonClick:(id)
并实现这个委托
- (void)passValue:(NSString *)value
self.value.text =
NSLog(@"the get value is %@", value);
button的Click方法,打开窗口2,并将窗口2的delegate实现方法指向窗口1。
- (IBAction)buttonClick:(id)sender
ValueInputView *valueView = [[ValueInputView alloc] initWithNibName:@"ValueInputView" bundle:[NSBundle mainBundle]];
valueView.delegate =
[self setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:valueView animated:YES];
第二个窗口的实现
#import &UIKit/UIKit.h&
#import "UIViewPassValueDelegate.h"
@interface ValueInputView : UIViewController {
NSObject&UIViewPassValueDelegate& * delegate;
UITextField *_
@property(nonatomic, retain)IBOutlet UITextField *
@property(nonatomic, retain) NSObject&UIViewPassValueDelegate& * delegate;
- (IBAction)buttonClick:(id)
.m实现文件
#import "ValueInputView.h"
@implementation ValueInputView
@synthesize delegate;
@synthesize value = _
- (void)dealloc {
[self.value release];
[super dealloc];
- (IBAction)buttonClick:(id)sender
[delegate passValue:self.value.text];
NSLog(@"self.value.text is%@", self.value.text);
[self dismissModalViewControllerAnimated:YES];
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet =
阅读(...) 评论()用户名:rainbird2
文章数:285
评论数:1154
访问量:2297230
注册日期:
阅读量:1297
阅读量:3317
阅读量:442277
阅读量:1128458
51CTO推荐博文
&一直以为ios的http请求这块很简单应该不支持记录,保存,或者使用cookie,可是想当然归想当然,真用的时候,真研究了一下发现还真强大。经过一番的研究简单说一下我的理解:当你访问一个网站时,不管你愿意或者不愿意,NSURLRequest都会帮你主动记录下来你访问的站点设置的cookie,而且很负责任的,当你下次再访问这个站点时,NSURLRequest会拿着上次保存下来了的cookie继续去请求。这规律同样适用于ASIHTTPRequest。所以当你做一些基于认证的网络请求时,cookie不失为一个完美的解决方案。
那么怎么查看cookie呢?很简单:
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieJar cookies]) {
& &NSLog(@&%@&, cookie);
这样就列出了所有已保存的cookie,如果当前为空怎么办呢?随便请求一个url喽。
& NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@&&]
& & & & & & & & & & & & & & & & & & & & & &cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData&
& & & & & & & & & & & & & & & & & & & &timeoutInterval:3];
& [NSURLConnection sendSynchronousRequest:request&
& & & & & & & & & & & & returningResponse:nil
& & & & & & & & & & & & & & & & & & error:nil];
& NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
& for (NSHTTPCookie *cookie in [cookieJar cookies]) {
& & NSLog(@&%@&, cookie);
是不是得到了类似:
&NSHTTPCookie version:0 name:&PHPSESSID& value:&evf5rcboo8th1dnl53fs4ukmt2& expiresDate:(null) created: 14:28:13 +42e+08) sessionOnly:TRUE domain:&& path:&/& isSecure:FALSE&
的东东?这就是cookie啦
怎么清空cookie呢?
& NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
& NSArray *_tmpArray = [NSArray arrayWithArray:[cookieJar cookies]];
& for (id obj in _tmpArray) {
& & [cookieJar deleteCookie:obj];
这样cookie就消失的一干二净了。
会查看cookie了,也会清空cookie了,怎么设置指定的cookie呢?
& NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
& [cookieProperties setObject:@&username& forKey:NSHTTPCookieName];
& [cookieProperties setObject:@&rainbird& forKey:NSHTTPCookieValue];
& [cookieProperties setObject:@&& forKey:NSHTTPCookieDomain];
& [cookieProperties setObject:@&& forKey:NSHTTPCookieOriginURL];
& [cookieProperties setObject:@&/& forKey:NSHTTPCookiePath];
& [cookieProperties setObject:@&0& forKey:NSHTTPCookieVersion];
& NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
& [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
这样就可以了。输入一下,是不是得到了下面这样的结果:
&NSHTTPCookie version:0 name:&username& value:&rainbird& expiresDate:(null) created: 14:36:53 +42e+08) sessionOnly:TRUE domain:&& path:&/& isSecure:FALSE&
其实ios的cookie就这么简单。如果mac也支持这么干的话,那以后写点登陆的脚本不是简单多了:)本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)
11:11:39 15:08:54 18:13:06 17:38:06 16:58:22 11:04:50 14:20:28问题对人有帮助,内容完整,我也想知道答案
问题没有实际价值,缺少关键内容,没有改进余地
我用UIWebView加载出来了,但是换成WKWebView就加载失败,错误原因是post请求的参数没传过去。UIWebView的加载代码是这样的
UIWebView *webView = [[UIWebView alloc] init];
NSString *bodyShare = [NSString stringWithFormat: @"hID=%@", userID];
NSMutableURLRequest * requestShare = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:self.urlStr]];
[requestShare setHTTPMethod: @"POST"];
[requestShare setHTTPBody: [bodyShare dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest:requestShare];
换成WKWebView就加载不出来了,请教下各位,怎么样用WKWebView加载POST请求。
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
你确定你addSubView了?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
根据上面的代码,应该可以正常的发送请求。能否提供出错一个demo?
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
该答案已被忽略,原因:
答案对人有帮助,有参考价值
答案没帮助,是错误的答案,答非所问
请问博主,这个问题你解决了吗?如果解决,辛苦告诉我一下
该答案已被忽略,原因:
同步到新浪微博
分享到微博?
Hi,欢迎来到 SegmentFault 技术社区!⊙▽⊙ 在这里,你可以提出编程相关的疑惑,关注感兴趣的问题,对认可的回答投赞同票;大家会帮你解决编程的问题,和你探讨技术更新,为你的回答投上赞同票。
明天提醒我
关闭理由:
删除理由:
忽略理由:
推广(招聘、广告、SEO 等)方面的内容
与已有问题重复(请编辑该提问指向已有相同问题)
答非所问,不符合答题要求
宜作评论而非答案
带有人身攻击、辱骂、仇恨等违反条款的内容
无法获得确切结果的问题
非开发直接相关的问题
非技术提问的讨论型问题
其他原因(请补充说明)
我要该,理由是:
扫扫下载 App

我要回帖

更多关于 httpget 设置请求参数 的文章

 

随机推荐