【问题标题】:Error with Json http request iOSJson http请求iOS错误
【发布时间】:2015-05-05 19:19:33
【问题描述】:

我正在尝试开发 Livelink RESTful 服务并在 iOS 应用程序中获取数据。 api 的文档链接是:

https://developer.opentext.com/webaccess/#url=%2Fawd%2Fresources%2Fapis%2Fcontent-server-v1.1%23!%2Fauth

我用于获取响应数据的代码是:

NSDictionary *authDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                              @"XYZ",@"username",
                              @"ABC",@"password",
                              nil];
NSError *error;
NSData *requestData = [NSJSONSerialization dataWithJSONObject:authDict options:0 error:&error];
NSURL *url = [NSURL URLWithString:@"http://livelink.xyz.com/livelink/livelink.exe/api/v1/auth"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];                                              
[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];

NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding]; 

但是在请求回复中我没有得到身份验证结果。当我记录请求回复时,日志是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset></div>
</div>
</body>
</html>

您能帮我获得身份验证票吗?提前致谢。

【问题讨论】:

    标签: ios objective-c json internal-server-error nsmutableurlrequest


    【解决方案1】:

    我不太确定 iOS 的东西,但它可能对你有帮助。我正在使用 ajax 来获取票,它可以工作:

    <html>
      <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script>
          var url = "your url/api/v1";
          var credentials = { username: "USERNAME", password: "PASSWORD" };
    
          $.post(url + "/auth", credentials, function (response) {
            var ticket = response.ticket;
            alert(response)
          });
        </script>
      </head>
      <body>Hi</body>
    </html>
    

    希望对你有帮助。

    【讨论】:

    • 是的,我正在尝试在 iOS 代码中执行相同的步骤,但它不起作用。
    【解决方案2】:

    你需要改变:

    [request setValue:@"application/json charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    

    通过在两个值之间添加分号:

    [request setValue:@"application/json; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    

    我的建议是使用 Async:

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    
        NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding];
    
        NSLog(@"requestReply = \n[%@]", string);
    }];
    if (error)
    {
        NSLog(@"error = %@", error);
    }
    

    但您遇到的错误似乎与您尝试访问的服务器有关,请确保 http://livelink.xyz.com/livelink/livelink.exe/api/v1/auth 可访问且存在。请查看您的服务器配置。

    以下错误:

    500 - Internal server error.
    There is a problem with the resource you are looking for, and it cannot be displayed.
    

    此错误与服务器端有关,与您的客户端无关。

    如果你搜索错误的描述,看起来你使用的是微软网络服务器,请看一下:https://support.microsoft.com/en-us/kb/942031

    除此之外,您的代码对我来说还不错。

    【讨论】:

    • 但是下面的 iOSMobiler 回答中提供了同样的事情。它不适用于 iOS 代码。
    猜你喜欢
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2012-06-29
    • 2015-10-03
    相关资源
    最近更新 更多