我想在IOS中實現Http。如何在Objective-C中爲HTTP設置http標頭?
我使用以下代碼將GET
命令發送到服務器。
SERVER_IP = @"xxx.xx.xxx.xxx";
PORT = @"12345";
USER_ID = @"1234-1234-1234-1234-1234";
USER_PWD = @"123456";
url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@/message/user/%@",SERVER_IP,PORT,USER_ID]];
[request setHTTPMethod:@"GET"];
request = [NSURLRequest requestWithURL:url];
NSDictionary *header = [request allHTTPHeaderFields];
[header setValue:@"Authorization" forKey:USER_PWD];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",html);
}];
它在日誌處顯示**The resource requires authentication, which was not supplied with the request**
。
我已經設置了參數header
,但我不知道如何使用它。
有人可以幫助我,教我如何實施GET
命令和設置密碼?