怎么通过http向服务器发送数据并接受到返回的数据

1。一个网站上信息

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

NSString *string = @"http://服务器地址";

NSURL *connection = [[NSURL alloc] initWithString:string];

[request setURL:connection];

[request setHTTPMethod:@"GET"];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

你是想得到当前上传的进度?来显示进度条吧?
NSConnection 有一个自代的代理方法,可以作到这一点!

http://developer.apple.com/Mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

– connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:  delegate method这个可以作到

2。比较靠谱的信息

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
{
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值