【问题标题】:Can't POST a request to service无法发布服务请求
【发布时间】:2012-11-25 15:29:53
【问题描述】:

出于某种原因,我总是得到Endpoint not found.,但是当我把它放在浏览器中时,它可以完美运行。我肯定做错了什么..

- (void)requestLoad:(NSString *)req_udid Age:(NSString *)req_age Gender:(NSString *)req_gender CheckBoxes:(NSString *)req_checkBoxes
{
    NSString *post = [NSString stringWithFormat:@"/UpdatePersonalInterests/%@/%@/%@/%@/0",req_udid, req_age, req_gender, req_checkBoxes];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    //set up the request to the website
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    [request setURL:[NSURL URLWithString:NSLocalizedStringFromTable(@"kServiceURL", @"urls", nil)]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

    NSError *error;
    NSURLResponse *response;

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

    NSLog(@"%@",result);
}

谢谢!

【问题讨论】:

  • 找不到端点在哪里?
  • 在这里:NSString *result = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];NSLog(@"%@",result);
  • 您是否尝试将其放在网络浏览器地址字段中,或者您是否尝试过发布到相同的网址?也许该端点只是设置为接收 GET 请求
  • 没有真正关注您的问题。您是说 NSLog 打印出“找不到端点”吗?或者这是您在控制台上的其他错误中看到的内容。
  • NSLog 打印 result 即:Endpoint not found. - 结果应该是 "Operation Succeed""Operation Failed"

标签: objective-c ios nsurlconnection nsurl nsurlrequest


【解决方案1】:

您似乎正在使用自定义服务方案。您是否在 Target -> info - URLS Types 中注册了它?请参阅 Apple 文档或注册自定义 URL 方案:Implementing Custom URL Schemes

【讨论】:

  • 为什么需要注册?
【解决方案2】:

所以我已经设法用 NSURLConnection 和异步请求用这段代码做到了这一点:

- (void)getIntrests
{
    NSString *req_udid = [PROUtils createOrLoadUserIdentifier];
    NSString *webaddress = [kServiceBaseURL stringByAppendingString:[NSString stringWithFormat:@"/GetPersonalInterestsForUdid/%@",req_udid]];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:webaddress] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:
     ^(NSURLResponse* response, NSData* data, NSError* error) {
         NSString* dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

         [[NSNotificationCenter defaultCenter] postNotificationName:kGotMediaFromServer object:dataString];

         NSLog(@"Update response completed: %@ with data: %@ error: %@",response,dataString,error);
     }];
}

希望它对某人有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2015-09-21
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多