【问题标题】:Wrong parameters in POST requestPOST 请求中的错误参数
【发布时间】:2014-05-19 07:58:26
【问题描述】:

我是 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];
}

但服务器总是返回响应 {"result":"error"}

当我传递这个数据时,邮递员 - 返回“成功”

同样的请求,但在 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);

...并返回“成功”。

我错过了哪个参数?

UPD:

我已经明白问题出在哪里了。邮递员将“+”符号(在电话号码中)转换为“%2B”,但我的 POST 请求没有这样做。 那么我必须设置哪个参数来纠正这个问题?

【问题讨论】:

  • 在创建jsonData 时尝试分配您的NSError,并首先查看失败的原因。继续往下走

标签: android ios rest post


【解决方案1】:

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

我希望这会有所帮助, 干杯。

【讨论】:

  • 没有。 [NSURLConnection sendSynchronousRequest:request returnedResponse:&response error:nil] 返回 nil
【解决方案2】:

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

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);

NSLogRESULT:{ result = error; }

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

UPD。 使用下面的方法在 POST 之前转义字符串。

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

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

 return escapedString;

}

【讨论】:

  • 很奇怪。我已复制粘贴您的代码并得到错误的响应。你得到了什么回应?
  • 本文代码中没有的另一件事是初始化request 对象的位置。你有这样的NSMutableURLRequest *request; 还是这样的NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];?
  • 我认为你没有正确初始化你的NSMutableURLRequest,这就是为什么你的NSData总是nil
  • NSData 不是零。它 = {“结果”:“错误”}。但必须是 {"result":"success"} - 就像在 POSTMAN 请求模拟器中一样。
  • 由于某种原因它看起来不喜欢编码。您是否有权访问服务器日志文件或服务器从您的应用程序接收到的内容?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
  • 2022-01-04
  • 2013-01-05
  • 1970-01-01
  • 2018-04-10
相关资源
最近更新 更多