POST请求中的参数错误

时间:2014-05-19 07:58:27

标签: android ios rest post

我是iOS新手。

我使用2个键值参数发出请求:

NSString* params = @"sid=d2a8b790364944ef870ed94c1e4fdea3 & phone=+380503424248";
NSURL* url = [NSURL URLWithString:@"http://gps.privatbank.ua/auth/check"];

[request setURL:url];

request.HTTPMethod = @"POST";
request.HTTPBody = [params dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


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

NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;

NSString *result = [results valueForKey:@"result"];

if([result isEqualToString:@"error"]){
    authorizationViewController = [AuthorizationViewController new];
    [authorizationViewController openEnterPhone:self];
}

但是服务器总是返回响应{"结果":"错误"}

当我传递此数据时抛出邮差 - 返回"成功"

enter image description here

相同的请求,但在Android应用程序中看起来像:

HttpPost postMethod = new HttpPost("http://gps.privatbank.ua/auth/check");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

nameValuePairs.add(new BasicNameValuePair("sid", PreferenceController.getSsid(context)));
nameValuePairs.add(new BasicNameValuePair("phone", PreferenceManager.getDefaultSharedPreferences(context).getString("phone_number","")));

postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));

ResponseHandler<String> res = new BasicResponseHandler();

String response = getInstance().execute(postMethod, res);

...并返回&#34;成功&#34;。

我错过了哪个参数?

UPD:

我已经明白问题所在。邮差转换&#39; +&#39;符号(在电话号码中)到&#39;%2B&#39;,但我的POST请求不会这样做。 那么我必须设置哪个参数来纠正这个?

2 个答案:

答案 0 :(得分:0)

尝试 NSString* params = @"sid=d2a8b790364944ef870ed94c1e4fdea3&&phone=+380503424248";

我希望这有帮助, 干杯。

答案 1 :(得分:0)

使用以下代码,我能够从此服务器获得响应:

NSString* params = @"sid=d2a8b790364944ef870ed94c1e4fdea3&phone=+380503424248";
    NSURL* url = [NSURL URLWithString:@"http://gps.privatbank.ua/auth/check"];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];

    [request setURL:url];

    request.HTTPMethod = @"POST";
    request.HTTPBody = [params dataUsingEncoding:NSUTF8StringEncoding];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


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

    if(error)NSLog(@"ERROR:%@",error);

    NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil;
    NSString *result = error ? [NSString stringWithFormat:@"ERROR-->%@",error.localizedDescription] : results;

    NSLog(@"RESULT:%@",result);
NSLog

RESULT:{ result = error; }

我注意到的第一件事是你的params字符串在参数&之前和之后都有空格。删除空格,应该没问题。

<强> UPD。 在使用下面的方法发布它们之前,请先转义字符串。

-(NSString*)escapeString:(NSString*)string
{

 NSString *escapedString = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(__bridge CFStringRef)string, NULL, CFSTR("!*();:@&=+$./?%#[]"), kCFStringEncodingUTF8));

 return escapedString;

}