Windows程序(C# VS 2008)杂记之Java访问Webservice服务
来源:互联网 发布:java相关网络协议卡死 编辑:程序博客网 时间:2024/05/26 19:15
2014年3月20日星期四 海淀五路居 晴
从上周一开始,一直到昨天,被老大给借到别的组去弄文档,拿着仅会画笔工具的PS,不停地涂啊涂,这是拿着先进的工具生生地做着体力活,整个人都干的有些麻木了。
终于又可以帮别的同事打打下手了,实现Java对.netWebservice服务的访问。
1. 创建Webservice服务
1) 创建一个空的ASP.NET Web应用程序
2) 右键点击项目名称,添加新建项,选择Web模版中的Web服务
3) 点击调试,服务创建完成,我的地址为:
http://localhost:31058/WebService1.asmx
2. 编写Java客户端
packagecom.jackhally; importjava.net.URL; importjavax.xml.namespace.QName; importorg.apache.axis.client.Call; importorg.apache.axis.client.Service; public classWebserviceClient { publicstatic String getInfo() { Stringresult = ""; try{ //定义服务 Serviceservice = new Service(); //调用Hello方法 CallcallHelloWorld = (Call) service.createCall(); callHelloWorld.setOperationName(newQName("http://localhost:31058/WebService1.asmx", "HelloWorld")); //callHelloWorld.addParameter(newQName("http://localhost:31058/WebService1.asmx","name"),XMLType.XSD_STRING,ParameterMode.IN); //call.addParameter("name",XMLType.XSD_STRING,ParameterMode.IN); callHelloWorld.setTargetEndpointAddress(newURL("http://localhost:31058/WebService1.asmx")); result= (String)callHelloWorld.invoke(new Object[]{}); System.out.println("Theresult is " + result); } catch(Exceptione) { e.printStackTrace(); } return result; } }faultString: System.Web.Services.Protocols.SoapException:服务器未能识别 HTTP头 SOAPAction 的值:
解决方案:
ASP.NET端,增加引用using System.Web.Services.Protocols;
进行如下修改:
[WebService(Namespace = "http://tempuri.org/")]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]//添加这句
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
接着调用,报出可能报出如下错误:
faultString: System.Web.Services.Protocols.SoapException:无法识别请求元素 <HelloWorldxmlns='http://localhost:31058/WebService1.asmx'>。
faultString: org.xml.sax.SAXException: Processing instructions are not allowed within SOAP messages
解决方案:
//[WebService(Namespace = "http://tempuri.org/")]将地址改为我服务的地址
[WebService(Namespace = "http://localhost:31058/WebService1.asmx")]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
调用结果如下:
3. 发布Webservice服务到IIS,使同事可以远程调用
我的电脑->右键->管理
网站-右键-添加网站-随意输网站名称-物理路径定位到站点所在的文件夹
然后绑定本机ip地址和端口号,确定即可;
浏览器中输入:http://绑定ip地址:端口/WebService1.asmx 就可以访问了即http:://192.168.1.124:9004/WebService1.asmx
远程客户端访问服务器上的WebService的时候,可以显示上图,但点击调用相关的方法时显示“只能用于来自本地计算机的请求”,需要修改配置。修改Webservice服务下web.config文件,在system.web节点下面添加下面的节点:
<webServices >
<protocols >
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</protocols>
</webServices>4. 添加有参数的方法
ASP.NET服务端:
[WebMethod]
public string HelloWorld(string name)
{
return name;
}
Java客户端:
public static String getInfo(String name) { Stringresult = ""; try{ //定义服务 Service service = new Service(); //调用Hello方法 Call callHelloWorld = (Call) service.createCall(); //设置调用的方法 callHelloWorld.setOperationName(newQName("http://localhost:31058/WebService1.asmx", "HelloWorld")); //设置调用方法的参数 callHelloWorld.addParameter(newQName("http://localhost:31058/WebService1.asmx","name"),XMLType.XSD_STRING,ParameterMode.IN); //如果这样添加参数, //call.addParameter("name",XMLType.XSD_STRING,ParameterMode.IN); //则会报出如下错误: //faultString: org.xml.sax.SAXParseException: Content is not //allowedin prolog. callHelloWorld.setTargetEndpointAddress(newURL("http://localhost:31058/WebService1.asmx")); result = (String)callHelloWorld.invoke(new Object[]{name}); System.out.println("The result is " + result); } catch(Exceptione) { e.printStackTrace(); } return result;}
- Windows程序(C# VS 2008)杂记之Java访问Webservice服务
- Windows程序(C# VS 2008)杂记 之string和拼音
- Windows程序(C# VS 2008)杂记之拼音转汉字
- Windows程序(C# VS 2008)杂记之GDI+只能向右下奔跑的矩形
- Windows程序(C# VS 2008)杂记之智能设备项目打包CAB
- Windows程序(C# VS 2008)杂记之智能设备项目打包CAB
- Windows程序(C# VS 2008)杂记之重命名项目名、项目文件夹、命名空间
- c# windows服务程序
- c# windows服务程序
- C# Windows服务程序之添加安装程序图解
- 杂记(二):使用Http或Webservice访问其它Java项目
- C#中Winform程序调用WebService服务
- C#中Winform程序调用WebService服务
- java程序调用c# webservice
- 使用VS开发Windows服务程序
- c#写windows服务程序
- 安装C# Windows服务程序
- C# 创建Windows 服务程序
- 2014第12周-周结
- 这样做游戏还能做多久?(二)
- 如何解决Maven依赖本地仓库eclipse报错的问题
- POJ-2524 Ubiquitous Religions 并查集
- 考前预习1
- Windows程序(C# VS 2008)杂记之Java访问Webservice服务
- Octave入门
- 四层 七层负载均衡
- Linux中.a,.la,.o,.so文件的意义和编程实现
- How to debug a GLSL shader?
- 特殊的二进制数
- 单链表排序
- cocos2d-x 3.0rc开发指南:Windows下Android环境搭建
- 随机生成一个n bit位的长整数