您的位置:首页 > 大数据 > 人工智能

iPhone访问.Net WebService的3种方法

2012-08-02 19:18 218 查看
C# 代码

public class WebService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public bool Login(string email, string password)
{
return true;
}
}

1.Http Post

NSString *postString = [NSString stringWithFormat:@"email=%@&password=%@",inEmail, inPassword];
NSString *urlString = @"http://localhost/Webservice.asmx/Login";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];
[theRequest addValue: @"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [postString dataUsingEncoding:NSUTF8StringEncoding]];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *retStr = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",retStr);

2.Http Get
NSString *urlString = @"http://localhost/Webservice.asmx/Login?password=123&email=222";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *retStr = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",retStr);
3.SOAP
NSString *soapString = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope\n"
"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"
"<soap:Body>\n"
"<Login>\n"
"<email>%@</email>\n"
"<password>%@</password>\n"
"</Login>\n"
"</soap:Body>\n"
"</soap:Envelope>", inEmail,inPassword];

NSURL *url = [NSURL URLWithString:@"http://localhost/WebService.asmx"];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapString length]];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"/Login" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapString dataUsingEncoding:NSUTF8StringEncoding]];
NSData* urlData;
NSURLResponse *response;
NSError *error = nil;
urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString* retStr = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"%@",retStr);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息