OStack程序员社区-中国程序员成长平台

标题: ios - 如果通过 iOS 客户端发送请求,则无法识别参数,Curl 相同的 url 按预期工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:19
标题: ios - 如果通过 iOS 客户端发送请求,则无法识别参数,Curl 相同的 url 按预期工作

所以我从 ios 应用程序向我的服务器发出 GET 请求。我得到的响应是一个空数组,但我期待一些数据。我使用 Curl 到相同的 url(从日志中复制粘贴)并且我得到了一些数据,所以可能它是我的应用程序。但是当我更改 url 并尝试记录响应时,我确实得到了一个响应,表明我的应用程序很好。现在我不知道该怎么办。

我的服务器是一个 Rails 应用程序。代码如下:

  def search
  @query = params[:user]
  q="%#{@query}%"
  users = []

  @users = User.where("username like ?", q)
  for user in @users
    if (user.avatar)
      users << {username: user.username, avatar: user.avatar.image.thumbnail_dp}
    else
      users << {username: user.username, avatar: ""}
    end
  end
  respond_to do |format|
      format.json {render :json => users.to_json }
  end
end

这是我的 Objective-C 代码

-(void)getFriendsNSString *)query{

  Keychain *keychain = [[Keychain alloc]init];
  NSString *authToken = [keychain getAuthToken];
  NSString *url = @"http://url.json";
  NSString *urlString = [NSString stringWithFormat"%@?user=%@", url, query ];
  NSURL *main_url = [NSURL URLWithString:urlString];
  NSLog(@"url = %@", main_url);
  self.remote_response = [[NSMutableData alloc] init];

  NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
  [request setURL: main_url];
  [request setHTTPMethod"GET"];
  [request setValue"application/json" forHTTPHeaderField"Content-type"];
  [request setValue"application/json" forHTTPHeaderField"Accept"];
  [request setValue:authToken forHTTPHeaderField"X-AUTH-TOKEN"];
  NSLog(@"request ===%@", request);
  self.remote_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  [self.remote_connection start];
  [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

这里是委托(delegate)方法:

- (void)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response{
    [self.remote_response setLength:0];
    NSLog(@"DID RECEIVE RESPONSE");
}

- (void)connectionNSURLConnection *)connection didFailWithErrorNSError *)error {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle"Error"
                                               message@"Could not get friends with error %@", error)
                                              delegate:self
                                     cancelButtonTitle"Ok"
                                     otherButtonTitles:nil , nil];
   [alert show];
}

- (void)connectionNSURLConnection *)connection didReceiveDataNSData *)d {
    [self.remote_response appendData:d];
    NSLog(@"Did receive data");
}

- (void)connectionDidFinishLoadingNSURLConnection *)connection {
    NSError *error;
    NSString *responseText = [[NSString alloc] initWithData:self.remote_response
                                               encoding:NSUTF8StringEncoding];
    NSData *data = [responseText dataUsingEncoding:NSASCIIStringEncoding];
    NSLog(@"Response ==> %@", responseText);
    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data
                                                                   options:NSJSONReadingMutableContainers error:&error];
    if([responseDictionary count]>0){
         NSLog(@"Response::::%@", responseDictionary);

    }
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

这是我得到的 NSURLResponse 日志

DID RECEIVE RESPONSE
Did receive data
Response ==> []

编辑::: 发现了一些东西。没有设置参数我不知道为什么当我 NSLog 时,我看到了参数。

如果我 curl ,这里是日志

2013-09-05T07:30:58.222613+00:00 app[web.1]:Started GET "/search.json?user=heeman" for 58.137.173.76 at 2013-09-05 07:30:58 +0000
2013-09-05T07:30:58.428178+00:00 app[web.1]: Processing by UsersController#search as JSON
2013-09-05T07:30:58.428178+00:00 app[web.1]:   Parameters: {"user"=>"heeman"}
2013-09-05T07:30:58.428178+00:00 app[web.1]: Completed 200 OK in 200ms (Views: 0.1ms | ActiveRecord: 7.5ms)

这是通过我的 ios 客户端的日志

2013-09-05T07:35:35.766319+00:00 app[web.1]: Started GET "/search.json?user=heeman" for      58.137.173.76 at 2013-09-05 07:35:35 +0000
2013-09-05T07:35:35.778962+00:00 heroku[router]: at=info method=GET path=/search.json?user=heeman host="host" fwd="58.137.173.76" dyno=web.1 connect=1ms service=51ms status=304 bytes=0
2013-09-05T07:35:35.773667+00:00 app[web.1]: Processing by UsersController#search as JSON
2013-09-05T07:35:35.773667+00:00 app[web.1]:   Parameters: {"user"=>{}}
2013-09-05T07:35:35.773667+00:00 app[web.1]: Completed 200 OK in 3ms (Views: 0.2ms | ActiveRecord: 1.2ms)

如果您在日志中看到,url 确实显示参数 method=GET path=/search.json?user=heeman 但随后 Parameters: {"user"=>{ }}



Best Answer-推荐答案


删除请求中的 Content-type 设置。您不包括任何数据主体,只包括参数,因为这是 GET,而不是 PUT 或 POST。 GET 请求不应包含 Content-type(它们应仅包含 Accept),尽管这不是硬性要求,其影响取决于所使用的服务器处理请求。

关于ios - 如果通过 iOS 客户端发送请求,则无法识别参数,Curl 相同的 url 按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629605/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4