zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    CodeForces666E Forensic Examination
    #46. 【清华集训2014】玄学
    #207. 共价大爷游长沙
    BZOJ4259残缺的字符串
    [六省联考2017]分手是祝愿
    BZOJ2616PERIODNI
    UVa 1363 Joseph's Problem (等差数列)
    UVa 1641 ASCII Area
    UVa 10213 How Many Pieces of Land? (组合数学 & 图论)
    UVa 1640 The Counting Problem (数位DP)
  • 原文地址:https://www.cnblogs.com/pipizhu/p/2005303.html
Copyright © 2011-2022 走看看