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
lynulzy
V2EX  ›  iDev

今天遇到了一个奇怪的问题,关于系统的同步网络请求

  •  
  •   lynulzy · 2015-03-27 10:54:56 +08:00 · 2152 次点击
    这是一个创建于 3311 天前的主题,其中的信息可能已经有所发展或是发生改变。
    以前阅读文档的时候了解到发送网络请求时候如果使用connection的init方法会自动发起一次请求,之后再发送start消息的时候会再次产生一个请求。
    而在我的项目中使用了同步请求,代码如下
    <pre>
    NSURL *url = [NSURL URLWithString:reqData.url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; //????
    NSURLResponse *t_response = nil;
    NSError *t_err = nil;
    if (HttpConnMethod_GET == reqData.httpMethod) {
    [request setHTTPMethod:@"GET"];
    } else {
    NSData *postData = [self generateFormDataFromPostDictionary];
    [request setHTTPMethod:@"POST"];
    [request setValue:MULTIPART forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    }
    NSData *rcvData = [NSURLConnection sendSynchronousRequest:request returningResponse:&t_response error:&t_err];
    rcvStr = [[NSString alloc] initWithData:rcvData encoding:NSUTF8StringEncoding];
    //TODO:
    rcvDict = [NSJSONSerialization JSONObjectWithData:rcvData options:NSJSONReadingMutableContainers error:nil];
    err = t_err;
    <code>
    问题来了,sendSynchronousRequest方法调用之后抓包会发现此方法会产生两个请求,有什么方法解决?
    4 条回复    2015-03-27 13:30:31 +08:00
    lynulzy
        1
    lynulzy  
    OP
       2015-03-27 10:55:11 +08:00
    额,不支持markdown吗?
    elden
        2
    elden  
       2015-03-27 12:16:48 +08:00
    如果是POST请求,先看看第一个Response是不是100 Continue响应。
    lynulzy
        3
    lynulzy  
    OP
       2015-03-27 12:51:01 +08:00
    @elden 同步请求,不是异步的
    elden
        4
    elden  
       2015-03-27 13:30:31 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2826 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:57 · PVG 21:57 · LAX 06:57 · JFK 09:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.