我需要为 URL 连接设置 Cookie。我是安卓新手。在 iphone 中完成如下:
NSURL *serverURL = [NSURL URLWithString:@"<Server URL>"];
NSHTTPCookie *usernamecookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[serverURL host], NSHTTPCookieDomain,
[serverURL path], NSHTTPCookiePath,
@"username", NSHTTPCookieName,
@"<username>", NSHTTPCookieValue,
nil]];
NSHTTPCookie *passwordcookie = [NSHTTPCookie cookieWithProperties:
[NSDictionary dictionaryWithObjectsAndKeys:
[serverURL host], NSHTTPCookieDomain,
[serverURL path], NSHTTPCookiePath,
@"password", NSHTTPCookieName,
@"<password>", NSHTTPCookieValue,
nil]];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:usernamecookie];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:passwordcookie];
NSData *responseData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:serverURL] returningResponse:nil error:nil];
NSString *response = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]autorelease];
NSLog(@"response data %@",[response description]);
如果有人对此有任何想法,请提供帮助。