-2
NSString *username = @"user";
NSString *password = @"password";
NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
[dictionnary setObject:username forKey:@"user_email"];
[dictionnary setObject:password forKey:@"user_password"];

NSLog(@".....%@....",dictionnary);

NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
                                                   options:kNilOptions
                                                     error:&error];

NSString *urlString = @"http://abcd.com/SVCs/WSUserService.svc/MobSignIn";

NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];

[request setHTTPBody:jsonData];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection
                        sendSynchronousRequest:request
                        returningResponse:&response
                        error:&requestError];
NSString *responseString = [[NSString alloc]
                            initWithData:responseData
                            encoding:NSASCIIStringEncoding] ;
NSLog(@"%@", responseString);

我想将具有用户名和密码的 json 对象发布到 Web 服务.. 但它给出了一个模糊的输出.. 任何人都可以帮我解决这个问题

Output:

2013-05-14 18:50:17.155 UWUI[6226:11303] .....{
"user_email" = user;
"user_password" = password;
}....

2013-05-14 18:50:18.233 UWUI[6226:11303]  **

后跟一个xml格式的内容

**

4

2 回答 2

3
NSString *UN = @"user";
NSString *PWD = @"password";
NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
[dictionnary setObject:UN forKey:@"UN"];
[dictionnary setObject:PWD forKey:@"PWD"];

NSLog(@"dictionnary...%@", dictionnary);

NSError *error = nil;
NSData *jsonData =  [NSJSONSerialization dataWithJSONObject:dictionnary
                                                    options:kNilOptions
                                                      error:&error];


NSString *urlString = @"http://abcd.com/SVCs/WSUserService.svc/MobSignIn";

NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:jsonData];
NSURLResponse *response = NULL;
NSError *requestError = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
NSLog(@"%@", responseString);
于 2013-05-17T09:28:52.837 回答
1

设置内容类型可能会解决您的问题。在发送之前将给定的代码添加到您的请求中,

[request addValue:@"application/json" forHTTPHeaderField:@"Content-type"];
于 2013-05-14T13:48:30.293 回答