• Objectivec 模拟http请求


    这个是b比较成套的网络请求方法,网站:http://allseeing-i.com/ASIHTTPRequest/How-to-use
    如果用postfa方法提交则,
    NSURL
    *url = [NSURLURLWithString:@"http://192.168.0.120:8888/web/Login.php"];
    ASIFormDataRequest
    *request = [ASIFormDataRequestrequestWithURL:url];
    //ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
    [requestsetPostValue:@"userName"forKey:@"userName"];
    [requestsetPostValue:
    @"password"forKey:@"password"];
    //[request setDelegate:self];
    [requeststartSynchronous];
    NSError
    *error = [requesterror];
    if(!error) {
    NSString
    *response = [requestresponseString];
    NSLog(
    @"response = %@",response);
    }

    get方法:
    NSURL
    *url = [NSURL URLWithString:@"http://allseeing-i.com"];
    ASIHTTPRequest
    *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    NSError
    *error = [request error];
    if (!error) {
    NSString
    *response = [request responseString];
    }

    或者试用NSMutableURLRequest:
    NSString
    *myRequestString = @"&userName=baowu&password=123456";
    NSData
    *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
    NSMutableURLRequest
    *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://192.168.0.120:8888/web/Login.php"]];
    [ request setHTTPMethod:
    @"POST" ];
    [ request setHTTPBody: myRequestData ];
    NSData
    *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
    NSString
    * aStr = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
    NSLog(
    @"aStr = %@",aStr);
  • 相关阅读:
    Decrease (Judge ver.)
    Raising Modulo Numbers
    最短Hamilton路径
    64位整数乘法
    递归系列——数组和对象的相关递归
    函数内容新增——函数表达式
    数据结构和算法(一)——栈
    (转)jQuery中append(),prepend()与after(),before()的区别
    微信端的user-Agent
    less知识点总结(二)
  • 原文地址:https://www.cnblogs.com/pipizhu/p/2005303.html
Copyright © 2020-2023  润新知