我想调用以下web service,但想知道如何传递用户名和密码并获得回复。
答案 0 :(得分:0)
@interface ViewController ()
{
NSURLConnection *clearSession;
NSMutableData *receivedData;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self clearsession];
}
-(void)clearsession
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"https://mybusiness.expressoft.eu/webserviceintegration/v1/myshop/MyShopDataService.svc/"]];
NSString *requestString = [NSString stringWithFormat:@"Username=karthik&Password=abcdesg",nil];
NSLog(@" RequestString for get card details: %@",requestString);
NSMutableData *requestData =[NSMutableData dataWithBytes:[requestString UTF8String] length: [requestString length]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
clearSession = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (clearSession) {
NSLog(@"data sent ");
} else
{
NSLog(@"Not sent");
}
[clearSession start];
}
-(void)connection:(NSConnection*)conn didReceiveResponse:(NSURLResponse *)response
{
if (receivedData == NULL)
{
receivedData = [[NSMutableData alloc] init];
}
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
UIAlertView *customAlert = [[UIAlertView alloc]initWithTitle:@"No NetWork" message:@"Interet Connection is Lost" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[customAlert show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (connection==clearSession)
{
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:receivedData options: kNilOptions error:nil];
NSString *tmp=[[NSString alloc]initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", tmp);
NSLog(@" parsing JSON of add checkout: %@", jsonDict);
}
}