Windows程序(C# VS 2008)杂记之Java访问Webservice服务

本文档详细记录了如何使用Java访问.NET编写的Webservice服务,包括创建Webservice服务、解决SOAPAction识别问题、发布到IIS及处理远程调用限制,还涉及到有参数方法的添加和调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值