我看到了关于cookie的所有帖子......仍然无法使其工作......可能是我缺少一些概念..通过苹果文档和堆栈qns ... 我想加载一个网址。" http://example1.com/path1"在postview方法的webview中,我必须发布一个cookie。 一个cookie,它传递一个名为" token"的值。 avd value" abcde1234"
首先设置接受cookie的应用程序
- (void)applicationDidBecomeActive:(UIApplication *)application{
[[NSHTTPCookieStorage sharedHTTPCookieStorage]setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
}
所以我创建了一个cookie ....
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"token" forKey:NSHTTPCookieName];
[cookieProperties setObject:@"abcde1234" forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"http://example1.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"http://example1.com/path1" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieSecure];
使用
记录值NSHTTPCookie *cookie1;
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie1 in [cookieJar cookies]) {
NSLog(@"%@", cookie1);
}
我可以看到那里的饼干很好...... 我正在webview中加载网址" http://example1.com/path1"如
NSURL *url = [NSURL URLWithString:@"http://example1.com/path1"];
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
[requestObj setHTTPMethod:@"Post"];
[requestObj setHTTPShouldHandleCookies:YES];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
...但是网络显示缺少参数的例外....这是我应该输入的cookie。 我的问题是如何设置并使所有这些工作?这是如何为网址设置cookie,并在webview中加载post方法??
答案 0 :(得分:1)
我自己解决了问题... 帖子评论在这里被称为但是错过了声明 设置邮政请求正文..
NSString *string = [NSString stringWithFormat: @"param=%@¶m1=%@", val1,val2];
[requestObj setHTTPBody: [string dataUsingEncoding: NSUTF8StringEncoding]];
Cookie设置正确..
不删除问题,因为它可以用作ref:)