iOS中POST请求的格式

时间:2014-04-29 12:15:10

标签: ios json post

只有当参数为空时,才能通过POST请求获取iOS代码中的JSON响应。服务器的响应是{Token ="" }

NSString *postData = [NSString stringWithFormat:@""];

但是当我添加如下所示的任何参数时,我会得到400状态代码作为响应。

NSString *postData = [NSString stringWithFormat:@"userName=aps@test.com&deviceCode=bhj234&pwd=1234"];

有趣的是,相同的参数在REST Client中完美运行并获得响应。并且也适用于Android代码。在android中创建一个JSON对象,然后将这些键值对添加到JSON对象

JSONObject jsonObj = new JSONObject();
jsonObj.put("userName", "apple@test.com");
jsonObj.put("deviceCode", "Dev455");
jsonObj.put("pwd", "225");
StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
httppost.setEntity(entity);

并请求然后收到正确的回复。

有人可以建议我使用上述Android代码的iOS等效代码吗? 我现在的代码是

NSMutableString *URL=[[NSMutableString alloc] initWithString:@"http://***.***.**.***/Serv.svc/Login"];
    NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];

    NSString *postData = [NSString stringWithFormat:@"userName=aps@test.com&deviceCode=bhj234&pwd=1234"];

    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

    NSLog(@"post:%@",postData);

    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    NSLog(@"post:%@",[postData dataUsingEncoding:NSUTF8StringEncoding]);

    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];


    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

3 个答案:

答案 0 :(得分:0)

如果其他所有内容都不起作用,请尝试在url中传递参数:

    NSString *URL=[[[NSMutableString alloc] initWithString:@"http://***.***.**.***/Serv.svc/Login?userName=aps@test.com&deviceCode=bhj234&pwd=1234"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];

    NSLog(@"post:%@",URL);

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

一旦它对我有用,但是这当然不能回答你的代码有什么问题。

答案 1 :(得分:0)

用户AFNetworking并在您的课程中导入以下内容

#import "AFNetworking.h"
#import "AFHTTPRequestOperationManager.h"
#import "AFHTTPRequestOperation.h"

使用以下代码发布请求:

- (void)sendPOSTRequest {
NSArray *keys = [NSArray arrayWithObjects:@"userName",@"deviceCode",@"pwd",nil];
NSArray *objects = [NSArray arrayWithObjects:@"apple@test.com",@"Dev455",@"225", nil];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjects:objects forKeys:keys];


AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[manager POST:@"yourURL" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSError * error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:[operation responseData] options:0 error:&error];
    NSLog(@"JSON: %@", json);



} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
}

答案 2 :(得分:0)

我找到了问题的答案。

NSMutableDictionary *dict = [NSMutableDictionary dictionary];

[dict setValue:@"aps@test.com" forKey:@"userName"];
[dict setValue:@"vikkj107" forKey:@"deviceCode"];
[dict setValue:@"1234" forKey:@"pwd"];


NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:NSJSONReadingMutableLeaves error:nil];

NSMutableString *URL=[[NSMutableString alloc] initWithString:@"http://***.***.**.***/Serv/Register"];

NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:jsonData];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *resultStr=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];