V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
sunjws
V2EX  ›  iDev

AFNetWorking 请求每次进 failure, status code: 500

  •  
  •   sunjws ·
    sungys · 2015-07-08 10:41:15 +08:00 · 4930 次点击
    这是一个创建于 3228 天前的主题,其中的信息可能已经有所发展或是发生改变。
    不多说,看代码



    回调以后每次都走近failure里,查看error报错信息为
    URL: [url]http://192.168.xxx.xx/homework/service/user/authorize.aspx[/url] } { status code: 500, headers {
        "Cache-Control" = private;
        "Content-Length" = 2915;
        "Content-Type" = "text/html; charset=utf-8";
        Date = "Wed, 08 Jul 2015 00:36:38 GMT";
        Server = "Microsoft-IIS/6.0";
        "X-AspNet-Version" = "2.0.50727";
        "X-Powered-By" = "ASP.NET";
    }
    在网上搜了很多也没有解决,求指点
    第 1 条附言  ·  2015-07-08 13:38:41 +08:00
    问题已经解决,谢谢各位的指点
    只是加了这样一句
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    11 条回复    2015-07-08 15:22:21 +08:00
    wy315700
        1
    wy315700  
       2015-07-08 10:43:12 +08:00
    status code: 500 服务器报错了。。
    sunjws
        2
    sunjws  
    OP
       2015-07-08 10:51:17 +08:00
    @wy315700 同样的接口 自己写了一个简单的代码测试后,能够正常解析出数据

    //用这个方法请求接口能够正常返回数据
    -(NSData *)returnDownLoadWithJsonRequestStr:(NSString *)json Url:(NSString *)url
    {
    //第一步,创建URL
    NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"%@",url]];
    //第二步,创建请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url1 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setHTTPMethod:@"POST"];//设置请求方式为POST,默认为GET
    //NSString *str = [NSString stringWithFormat:@"%@",json];//设置参数
    NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:data];
    //第三步,连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    // NSLog(@"aaaaaa = %@",received);
    return received;
    }
    laoyur
        3
    laoyur  
       2015-07-08 11:02:39 +08:00
    自己抓包看两个请求有啥不同
    hyzjshwo
        4
    hyzjshwo  
       2015-07-08 11:20:15 +08:00
    看到192.168.xxx.xx 这样的段位,我问问是不是在外网调试?
    sobigfish
        5
    sobigfish  
       2015-07-08 11:25:55 +08:00   ❤️ 1
    500肯定先查服务端,你先把两个(AFNetWorking和自己写的)请求都直接dump出来看看区别 而且两个请求的格式都不一样 text/html application/json
    jianzong
        6
    jianzong  
       2015-07-08 12:23:00 +08:00
    可以先用postman确定是不是api服务器的问题
    dorentus
        7
    dorentus  
       2015-07-08 12:25:42 +08:00
    gist 里面的 Request:
    - Content-Type 没设置
    - 设置了 Accept: text/html (但是这不合理,因为后面的代码里面明显可以看出你要的是 application/json 的返回数据)
    - paramRequest 这个 Dictionary 是用表单参数的方式序列化为 POST body 来提交的

    下面你手写的代码:
    - Content-Type 设置成了 application/json
    - Accept 没有设置
    - paramRequest 这个 Dictionary 是序列号成 JSON 格式的字符串然后直接作为 POST body 提交的

    ----
    前两点不同,取决于你服务端的设置,可能会导致出错,也可能不出错
    第三点,直接导致你服务端收到的数据是完全不一样的;看起来,你的服务端期待的是 POST body 直接是 JSON 格式这种形式。
    pupboss
        8
    pupboss  
       2015-07-08 12:25:52 +08:00
    AFN 默认不支持 text/html,找到之后添加上就好了,估计是这个原因
    PopeyeLau
        9
    PopeyeLau  
       2015-07-08 12:55:00 +08:00
    `self.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"text/html", @"application/json",nil];`
    sunjws
        10
    sunjws  
    OP
       2015-07-08 13:29:59 +08:00
    @dorentus 对,服务端期待的是POST body 直接是 JSON 格式这种形式。那么请教一下用AFN该怎么配置呢
    dorentus
        11
    dorentus  
       2015-07-08 15:22:21 +08:00
    @sunjws 设置一下 requestSerializer 应该就可以了
    _sharedClient.requestSerializer = [AFJSONRequestSerializer serializer];
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1095 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 19:14 · PVG 03:14 · LAX 12:14 · JFK 15:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.