iPhone访问.Net WebService的3种方法
2012-08-02 19:18
218 查看
C# 代码
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);
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);
相关文章推荐
- kSoap访问.Net的WebService的基本方法及问题
- 无需.net和专用组件实现用asp访问webservice
- .net程序出现没有对Temporary ASP.NET Files”的写访问权限解决方法
- .Net下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) [轉]Redfox
- Jquery调用webService远程访问出错的解决方法
- android ksoap2调用.net Webservice 方法总结
- WebService java 调用 .net方法
- 递归访问二叉树的3种遍历方法
- 在Android中访问WebService接口的方法
- flex访问同一个webService的不同方法
- Android客户端WebService访问接口(Ksoap2Android调用.net服务端)
- Flex与.NET互操作(三):基于WebService的数据访问(下)
- 成静态页面 .net中 最简单的3种方法
- map的3种访问方法
- 一起谈.NET技术,Flex 数据访问 WebService (上)
- 【转】Spring的WebServiceTemplate访问WebService的方法及其本质原理
- 一起谈.NET技术,Silverlight访问Apache服务器(Tomcat,Geronimo)中部署的Webservice
- java使用AXIS调用.net的webservice的小说明(webservice方法里含有对象)
- java 访问.net webservice返回的数据集
- <转载>在.NET中运行外部程序的3种方法