zoukankan      html  css  js  c++  java
  • NSMutableURLRequest和NSHTTPURLResponse的简单使用

    NSMutableURLRequest通过POST方法向服务器请求时间,很简单的一个例子,使用HTTP入门。

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *urlString = [[NSString alloc] initWithFormat:@"http://api.air-id.net/InterFace/datetime.php"];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@""] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postBody];


    NSHTTPURLResponse* urlResponse = nil;
    NSError *error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"服务器返回:%@",result);
    }

    发送XML数据:

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSString *urlString = [NSString stringWithFormat:@"http://api.air-id.net/InterFace/"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *contentType = [NSString stringWithFormat:@"text/xml"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"<boot>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"<package>SAC.MobileClient.Client</package>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"<class>RESOLVE_MASTER</class>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"<mac_code>55555555555555</mac_code>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"<imei>000000000000000</imei>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"<system>ios</system>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"</boot>"] dataUsingEncoding:NSUTF8StringEncoding]];


    [request setHTTPBody:postBody];
    NSLog(@"sendBody:%@",postBody);

    NSHTTPURLResponse* urlResponse = nil;
    NSError *error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"result:%@",result);

    }




  • 相关阅读:
    hdoj 2041 超级楼梯
    hdoj 刚入门~把11页A了一些~~
    编写Powerdesigner脚本,快速生成数据库表
    网站
    duwamish
    面试题
    http://www.sqlclub.com
    Microsoft PetShop 3.0 设计与实现数据访问层
    Java代码查询站点
    从Blog上面去掉那该死的“狗狗订阅”的logo !
  • 原文地址:https://www.cnblogs.com/foxmin/p/2402947.html
Copyright © 2011-2022 走看看