1。后台利用 cxf 构建一个web service服务。
-
-
-
- package com.alcor.jws.test;
-
- import javax.jws.WebMethod;
- import javax.jws.WebService;
-
- import org.apache.cxf.feature.Features;
-
-
-
-
-
-
- @WebService
- @Features(features = "org.apache.cxf.feature.LoggingFeature")
- public interface HelloWorld {
-
- @WebMethod
- String sayHi(String text);
- @WebMethod
-
- boolean userLogon(String username,String userpasswd);
- }
-
-
-
- package com.alcor.jws.test;
-
- import org.apache.cxf.feature.Features;
- import org.apache.log4j.Logger;
-
- import javax.jws.WebMethod;
- import javax.jws.WebService;
-
-
-
-
-
-
-
- @WebService
- @Features(features = "org.apache.cxf.feature.LoggingFeature")
- public class HelloWorldImpl implements HelloWorld {
-
-
-
- private static final Logger logger = Logger.getLogger(HelloWorldImpl.class);
-
-
- @WebMethod
- public String sayHi(String text) {
- if (logger.isDebugEnabled()) {
- logger.debug("sayHi(String) - start");
- }
-
- String returnString = "Hello,你好: " + text;
- if (logger.isDebugEnabled()) {
- logger.debug("返回内容:"+returnString);
- logger.debug("sayHi(String) - end");
- }
- return returnString;
- }
-
- @WebMethod
- public boolean userLogon(String username ,String userpasswd)
- {
- logger.debug("用户名是:"+username+"口令是:"+userpasswd);
- if (username.equalsIgnoreCase("admin"))
- {
- return true;
- }else{
- return false;
- }
- }
- }
-
-
-
- package com.alcor.jws.test;
-
- import org.apache.cxf.interceptor.LoggingInInterceptor;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
-
-
-
-
-
-
- public class Client {
-
- private Client() {
- }
-
- public static void main(String args[]) throws Exception {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- factory.setAddress("http://localhost:8080/SampleWebService/webservice/HelloWorld");
- factory.setServiceClass(HelloWorld.class);
- factory.getInInterceptors().add(new LoggingInInterceptor());
- HelloWorld helloWorld = (HelloWorld) factory.create();
- boolean msg = helloWorld.userLogon("admin","World");
- System.out.println(msg);
- }
- }
2。iphone 客户端的编程
-
-
-
-
-
-
-
-
- #import <UIKit/UIKit.h>
-
-
- @interface LogonViewController : UIViewController<NSXMLParserDelegate>
- {
- IBOutlet UITextField * userNameTextField;
- IBOutlet UITextField * userPasswordTextField;
- IBOutlet UIButton * userLogonButton;
- IBOutlet UITextField * webServiceURL;
- NSXMLParser *xmlParser;
- BOOL logonResult;
- NSMutableString *soapResults;
- }
-
- @property(nonatomic,retain) IBOutlet UITextField * userNameTextField;
- @property(nonatomic,retain) IBOutlet UITextField * userPasswordTextField;
- @property(nonatomic,retain) IBOutlet UIButton * userLogonButton;
- @property(nonatomic,retain) IBOutlet UITextField * webServiceURL;
- @property(nonatomic, retain) NSXMLParser *xmlParser;
- @property(nonatomic,retain) NSMutableString * soapResults;
-
- -(IBAction) logonButtonClick:(id)sender;
- @end
相关文章 :
- 手把手教你做 iphone的soap应用
-
分享一个用Xcode4实现基于Webservice用户登录的iphone程序
其中要注意的几点。
-
- <ns1:userLogon xmlns:ns1=\"http://localhost:8080/SampleWebService/webservice/HelloWorld/\">"
- "<arg0>%@</arg0>"
- "<arg1>%@</arg1>"
- "</ns1:userLogon>"
中web service 中的 userLogon方法的调用 要用ns1来引用。
- 传递的参数 不能和 webservice的变量名来写。而只能写成 arg0 和 arg1 这种方式。我查阅其他网上资料,都是写成<username>和<userpasswd>这种element的形式。但是在我的这个演示中,如果写成这种方式。后台会无法获得传入的变量。而用arg0 这种方式是可以传入。我不清楚是否是和 java cxf的webservice搭建环境和版本有关。
注意:如果后台的WebService是.net开发的,调用的程序略有不同:
例如 后台的ws服务链接是 :http://10.100.111.231:9000/MobileService.asmx
那么访问这个url,会返回如下内容:
这个页面提示了SOAP 1.1 和 SOAP 1.2 的调用的内容
- SOAP 1.1
-
- The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.
-
- POST /MobileService.asmx HTTP/1.1
- Host: 10.100.111.231
- Content-Type: text/xml; charset=utf-8
- Content-Length: length
- SOAPAction: "http://tempuri.org/Logon"
-
- <?xml version="1.0" encoding="utf-8"?>
- <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
- <soap:Body>
- <Logon xmlns="http://tempuri.org/">
- <userName>string</userName>
- <password>string</password>
- </Logon>
- </soap:Body>
- </soap:Envelope>
-
- HTTP/1.1 200 OK
- Content-Type: text/xml; charset=utf-8
- Content-Length: length
-
- <?xml version="1.0" encoding="utf-8"?>
- <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
- <soap:Body>
- <LogonResponse xmlns="http://tempuri.org/">
- <LogonResult>string</LogonResult>
- </LogonResponse>
- </soap:Body>
- </soap:Envelope>
-
- SOAP 1.2
-
- The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.
-
- POST /MobileService.asmx HTTP/1.1
- Host: 10.100.111.231
- Content-Type: application/soap+xml; charset=utf-8
- Content-Length: length
-
- <?xml version="1.0" encoding="utf-8"?>
- <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <soap12:Body>
- <Logon xmlns="http://tempuri.org/">
- <userName>string</userName>
- <password>string</password>
- </Logon>
- </soap12:Body>
- </soap12:Envelope>
-
- HTTP/1.1 200 OK
- Content-Type: application/soap+xml; charset=utf-8
- Content-Length: length
-
- <?xml version="1.0" encoding="utf-8"?>
- <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <soap12:Body>
- <LogonResponse xmlns="http://tempuri.org/">
- <LogonResult>string</LogonResult>
- </LogonResponse>
- </soap12:Body>
- </soap12:Envelope>
那么,我们在objectiveC中代码应该写成
static NSString * wsURL = @"http://10.100.111.231:9000/MobileService.asmx";
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
"<soap:Body>\n"
"<Logon xmlns=\"http://tempuri.org/\">"
"<userName>%@</userName>"
"<password>%@</password>"
"</Logon>"
"</soap:Body>\n"
"</soap:Envelope>",self.userNameTextField.text,self.userPasswordTextField.text];
NSLog(@"调用webserivce的字符串是:%@",soapMessage);
//请求发送到的路径
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",wsURL]];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
//以下对请求信息添加属性前四句是必有的,
[urlRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue:@"http://tempuri.org/Logon" forHTTPHeaderField:@"SOAPAction"];
NSLog(@"SOAPAction is %@ ",@"http://tempuri.org/Logon");
[urlRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
//请求
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
theConnection = nil;
注意红色的代码,这个http://tempurl.org/Logon 是要和 .net中的代码一致。否则无法访问。