C#调用java-ws(带身份验证功能)
2012-07-28 15:39
447 查看
java服务端:
服务接口:
@WebService @SOAPBinding(style = Style.RPC) public interface HelloWorld{ @WebMethod String getHelloWorldAsString(); }
服务实现:
@WebService(endpointInterface = "com.mkyong.ws.HelloWorld") public class HelloWorldImpl implements HelloWorld{ @Resource WebServiceContext wsctx; @Override public String getHelloWorldAsString() { MessageContext mctx = wsctx.getMessageContext(); //提取http header中的属性做权限判断 Map http_headers = (Map) mctx.get(MessageContext.HTTP_REQUEST_HEADERS); List userList = (List) http_headers.get("Username"); List passList = (List) http_headers.get("Password"); String username = ""; String password = ""; if(userList!=null){ //get username username = userList.get(0).toString(); } if(passList!=null){ //get password password = passList.get(0).toString(); } //Should validate username and password with database if (username.equals("mkyong") && password.equals("password")){ return "Hello World JAX-WS - Valid User!"; }else{ return "Unknown User!"; } } }
服务配置
/WebContent/WEB-INF/sun-jaxws.xml<?xml version="1.0" encoding="UTF-8"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="HelloWorld"
implementation="com.mkyong.ws.HelloWorldImpl"
url-pattern="/hello"/>
</endpoints>
/WebContent/WEB-INF/web.xml
<web-app> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>hello</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
C#客户端
引用部署在tomcat中java web服务的地址(http://localhsot:8080/WebServices/hello?wsdl)生成代理类,在生成的代理类中重载GetWebRequest方法:protected override System.Net.WebRequest GetWebRequest(Uri uri) { System.Net.WebRequest request = base.GetWebRequest(uri); request.Headers.Add("Username", "mkyong"); request.Headers.Add("Password", "password"); return request; }
在客户端中调用:
localhost.HelloWorldImplService service = new TestCA.localhost.HelloWorldImplService(); MessageBox.Show(service.getHelloWorldAsString());
如果没有设置username和password或者设置错误,则调用时服务端方法会提示错误:
设置正确提示:
相关文章推荐
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- c#调用Java的webservice出现身份验证失败的解决方案
- Java调用WebService接口实现发送手机短信验证码功能
- 在Flash中调用WS时的身份验证_5:将数组传递到ws
- C#调用Web Service时的身份验证
- Atitit java c# php c++ js跨语言调用matlab实现边缘检测等功能attilax总结
- Java调用WebService接口实现发送手机短信验证码功能
- 在Flash中调用WS时的身份验证_6:ws调用的用户验证和授权访问
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
- Java WS和C#调用
- 在Flash中调用WS时的身份验证_5:dataProvider的问题
- C#调用Web Service时的身份验证
- Java调用WebService接口实现发送手机短信验证码功能
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
- 在Flash中调用WS时的身份验证_6:客户端
- Java调用WebService接口实现发送手机短信验证码功能
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
- 万能的Java WebService调用程序支持C# JAVA WS调用
- C#调用Web Service时的身份验证
- C#调用Web Service时的身份验证