iphone发送数据到服务器端有多种方法

来源:互联网 发布:win10优化工具 推荐 编辑:程序博客网 时间:2024/06/05 00:14
 
iphone发送数据到服务器端有多种方法:
 
 
1、普通得http POST 方法,

 

iPhone HTTP Post发送数据

-(NSString *)MyUrl:(NSString *)urlStr

{

    NSURL *url;

    NSMutableURLRequest *urlRequest;

    NSMutableData *postBody = [NSMutableData data];

    url = [NSURL URLWithString:@"http://ceshixieyi.appspot.com/ceshixieyi..."];

    urlRequest = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease];

    [urlRequest setHTTPMethod:@"POST"];

    //NSString *udid = @"U0020   #999999#E3CEB5881A0A1FDAAD01296D7554868D#";

    [postBody appendData:[urlStr dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion:YES]];

    [urlRequest setHTTPBody:postBody];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];

   

NSString *result = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

if(returnData)

    {

NSLog(@"%@",result);

    }

else

{

NSLog(@"error!");

}

return result;

}


是这样调用的NSString *udid = @"U0020   #999999#E3CEB5881A0A1FDAAD01296D7554868D#";

[self MyUrl:udid];


2、使用SOAP方法

NSMutableString *sRequest = [[NSMutableString alloc] init];
     // Create the SOAP body
    [sRequest appendString:@"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">"];
    // This is the header section it is expecting in the body
    [sRequest appendString:@"<s:Header>"];
    [sRequest appendString:@"<o:Security s:mustUnderstand=\"1\" xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"];
    // This is where you pass in the user id and password.  This is sent in the body header as clear text, but since you will be using HTTPS it will be somewhat encrypted.
    [sRequest appendString:@"<o:UsernameToken>"];
    [sRequest appendString:@"<o:Username>"];
    [sRequest appendString:@"DIFZ"];
    [sRequest appendString:@"</o:Username>"];
    [sRequest appendString:@"<o:Password>"];
    [sRequest appendString:@"TheBest!"];
    [sRequest appendString:@"</o:Password>"];
    [sRequest appendString:@"</o:UsernameToken>"];
    [sRequest appendString:@"</o:Security>"];
    [sRequest appendString:@"</s:Header>"];
    // This is the body where you can pass in any parameters the WCF service expects
    [sRequest appendString:@"<s:Body>"];
    [sRequest appendString:@"<GetWeeksList xmlns=\"http://tempuri.org/\"></GetWeeksList>"];
    [sRequest appendString:@"</s:Body>"];
    [sRequest appendString:@"</s:Envelope>"];
   
     // The URL of the Webserver
    NSURL *myWebserverURL = [NSURL URLWithString:@"https://[Server URL]/FootballPool_DAL/GameData.svc"];
    // Use the private method setAllowsAnyHTTPSCertificate:forHost:
    // to not validate the HTTPS certificate.  This is used if you are using a testing environment and have
    // a sample SSL certificate set up
    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[myWebserverURL host]];
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myWebserverURL];
    // Add the Required WCF Header Values.  This is what the WCF service expects in the header.
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"http://tempuri.org/IGameData/GetWeeksList" forHTTPHeaderField:@"SOAPAction"];
    // Set the action to Post
    [request setHTTPMethod:@"POST"];
    // Set the body
    [request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]];
    // Create the connection
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    // Check the connection object
    if(conn)
    {
      myMutableData=[[NSMutableData data] retain];
    }
    // Make this class the delegate so that the other connection events fire here.
    [NSURLConnection connectionWithRequest:request delegate:self];
   
    NSError *WSerror;
    NSURLResponse *WSresponse;
    // Execute the WCF Service and return the data in an NSMutableData object
    myMutableData = [NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror];
    // Return the data to the caller for processing
    return myMutableData;
}

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host
{
    return YES;
}

+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host
{
}