我正在尝试在我的iPhone App中创建登录。
NSURL *urlNew = [NSURL URLWithString:urlstring];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:urlNew];
NSString *msgLength = [NSString stringWithFormat:@"%d", [parameterString length]];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:length forHTTPHeaderField:@"Content-Length"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setHTTPBody: httpbody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
[connection start];
这就是我将数据发布到服务器的方式,它有效 - 我得到了回复。问题是,响应是登录端的代码,而不是“已保存”区域的代码。我想我必须创建cookie,以便网站知道我已登录。
我搜索了互联网,但没有找到任何有用的东西。那么当我登录时,如何创建cookie?当我要去“已保存”区域中的另一个链接时,我是否每次都要发布此cookie?
希望你能解决我的问题。
格尔茨
答案 0 :(得分:28)
试试这个
[theRequest setValue:@"myCookie" forHTTPHeaderField:@"Cookie"];
编辑:
OP想知道如何创建cookie。所以这里有一些代码
NSDictionary *cookieProperties = [NSDictionary dictionaryWithObjectsAndKeys:
@"domain.com", NSHTTPCookieDomain,
@"\\", NSHTTPCookiePath,
@"myCookie", NSHTTPCookieName,
@"1234", NSHTTPCookieValue,
nil];
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
NSArray* cookieArray = [NSArray arrayWithObject:cookie];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:cookieArray];
[request setAllHTTPHeaderFields:headers];
答案 1 :(得分:4)
不确定它是否仍然相关,但是如果有人发现它有用,这里是您在提出请求后获取cookie的方式。您应该在指定为connectionDidFinishLoading:
的委托的对象中实现选择器NSURLConnection
(在您的情况下为self):
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
在这个connectionDidFinishLoading:选择器中你可以像这样访问cookie:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSHTTPCookieStorage * storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray * cookies = [storage cookiesForURL:connection.currentRequest.URL];
for (NSHTTPCookie * cookie in cookies)
{
NSLog(@"%@=%@", cookie.name, cookie.value);
// Here you can store the value you are interested in for example
}
}
然后,这个存储的值可以用在这样的请求中:
[request setValue:[NSString stringWithFormat:@"%@=%@", cookieName, cookieValue] forHTTPHeaderField:@"Cookie"];
或更高级的setAllHTTPHeaderFields:
,但请记住在NSHTTPCookiePath
字段中使用正确的值,请参阅详细信息here
NSHTTPCookieStorage
也有选择器-setCookies:forURL:mainDocumentURL:
,也可以用来设置Cookie。
答案 2 :(得分:3)
我遇到了同样的问题,WKWebView在初始加载时在PHP页面上显示了cookie,但是在重新加载/刷新页面时cookie已被清除。这就是我为使其发挥作用所做的一切。
我的WKWebView与cookie设置:
WKWebViewConfiguration *webViewConfig = [[WKWebViewConfiguration alloc] init]; // empty for now
_awesomeWebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webViewConfig];
_awesomeWebView.UIDelegate = self;
[self.view addSubview:_awesomeWebView];
NSString *url = @"https://phpwebsitewithcookiehandling.com";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
// set all the cookies from my NSHTTPCookieStorage in the WKWebView too
[request setHTTPShouldHandleCookies:YES];
[request setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:[NSHTTPCookieStorage sharedHTTPCookieStorage].cookies]];
// set the cookie on the initial load
NSString *cookie = @"status=awesome";
[request setValue:cookie forHTTPHeaderField:@"Cookie"];
NSLog(@"cookie set in WKwebView header: %@", cookie);
[_awesomeWebView loadRequest:request];
在我的phpwebsitewithcookiehandling.com上,我使用以下内容验证它是否有效:
<?php
echo "<li>status is: " . $_COOKIE['status'];
// Here comes the magic which set the servers SET-COOKIE header (apparently) so it works on consecutive page loads
setcookie('status', $_COOKIE['status'], time() + (86400 * 30), '/');
echo '<li><a href="">Blank reload</a>';
echo '<li><a href="#" onclick="location.reload(true);">Javascript reload</a>';
exit;
?>
经过多次试验和错误,它对我有用。祝好运。 :)
答案 3 :(得分:0)
有源代码可以从NSHTTPURLResponse
中提取Cookie字符串:
static NSString *CookieFromResponse(NSHTTPURLResponse *response) {
NSArray<NSHTTPCookie *> *cookies = [NSHTTPCookie cookiesWithResponseHeaderFields:response.allHeaderFields forURL:response.URL];
NSMutableString *cookieStr = [NSMutableString string];
for (NSHTTPCookie *cookie in cookies) {
if (cookieStr.length) {
[cookieStr appendString:@"; "];
}
[cookieStr appendFormat:@"%@=%@", cookie.name, cookie.value];
}
return cookieStr.length ? cookieStr : nil;
}
并将Cookie字符串设置为NSMutableURLRequest
:
NSString *cookieStr = CookieFromResponse(response);
[request addValue:cookieStr forHTTPHeaderField:@"Cookie"];