Fork me on GitHub

socket编程 urlrequest使用

http://hi.baidu.com/bloodyfox/blog/item/cc2e2a2a572485275243c172.html

AppDelegate* appdele = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    NSString *Url = nil;
    Url = [NSString stringWithFormat:@"%@/decoder_control.cgi?command=1&user=%@&pwd=%@",
                  appdele.globalIpAdd,
                  appdele.globalUserName,
                  appdele.globalPassword];
    
    NSURL *             url;
    NSURLRequest *      request;
    url = [NSURL URLWithString:Url];
    request = [NSURLRequest requestWithURL:url];
    assert(request != nil);
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
    
    NSURLResponse *response = nil;

    if ([response statusCode] == 401) {

    //无法连接

    }

   else {

        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        NSLog([error code]);
        NSLog([error localizedDescription]);
        int err = [error code];
        if (err == NSURLErrorCannotConnectToHost) { //-1004
            //错误处理
        }

        //其他错误处理

        ......

    }

posted on 2012-05-17 08:56  pengyingh  阅读(237)  评论(0)    收藏  举报

导航