C#调用Java的Axis2发布的WebService

相关参考:

C#实现WebService

Java实现WebService(Axis2)[转发+部分实践]

 

 1. 添加服务引用,地址的写法为:http://localhost:8080/AfisWebService/services/CalculateService?wsdl,需注意大小写

 2.调用如下:

        privateJavaWebService.CalculateServicePortTypeClient jws = new JavaWebService.CalculateServicePortTypeClient();

 

        privatevoid btn_JavaUpload_Click(object sender, EventArgs e)

        {

           byte[] bytes = GetBytesByPath(@"G:\Temp\123.txt");//获取文件byte[]

           string fileName = GetFileName()+".txt";//文件名称

           bool ret = jws.uploadFile(fileName, bytes);

           if(ret)

               MessageBox.Show("return message is : success");

           else

               MessageBox.Show("return message is : failed");

        }

 

        privatevoid btn_JavaDownload_Click(object sender, EventArgs e)

        {

           byte[] data =jws.downloadFile("201303151657208593.txt");

            if(data.Length <= 0)

            {

               MessageBox.Show("return message is : failed");

            }

            else

            {

               FileStream fs = new FileStream(@"C:\tt.txt", FileMode.Create);

                //开始写入

               fs.Write(data, 0, data.Length);

 

               //清空缓冲区、关闭流

               fs.Flush();

               fs.Close();

               MessageBox.Show("return message is : success");

            }

        }

注意:Java中的DataHandlerC#中直接被转换成了byte[]

Java中的定义如下:

public boolean uploadFile(StringfileName,DataHandler dh);

public DataHandler downloadFile(StringFileName);

 

 3. 如果在C#中调用时出现如下错误:“无法加载协定为“JavaWebService.CalculateServicePortType”的终结点配置部分,因为找到了该协定的多个终结点配置。请按名称指示首选的终结点配置部分。”,是因为因为找到了该协定的多个终结点配置。在配置文件app.config中将多余的节点删除即可:

<?xml version="1.0"encoding="utf-8" ?>

<configuration>

   <system.serviceModel>

       <bindings>

           <basicHttpBinding>

               <binding name="AfisServiceSoap"closeTimeout="00:01:00" openTimeout="00:01:00"

                   receiveTimeout="00:10:00" sendTimeout="00:01:00"allowCookies="false"

                   bypassProxyOnLocal="false"hostNameComparisonMode="StrongWildcard"

                   maxBufferSize="1024000" maxBufferPoolSize="1024000"maxReceivedMessageSize="1024000"

                   messageEncoding="Text" textEncoding="utf-8"transferMode="Buffered"

                   useDefault W e b P r o x y ="true">

                   <readerQuotas maxDepth="32"maxStringContentLength="8192" maxArrayLength="1024000"

                       maxBytesPerRead="4096" maxNameTableCharCount="16384"/>

                   <security mode="None">

                        <transportclientCredentialType="None" proxyCredentialType="None"

                            realm="" />

                        <messageclientCredentialType="UserName" algorithmSuite="Default"/>

                   </security>

               </binding>

               <binding name="CalculateServiceSoap11Binding"closeTimeout="00:01:00"

                    openTimeout="00:01:00"receiveTimeout="00:10:00" sendTimeout="00:01:00"

                   allowCookies="false" bypassProxyOnLocal="false"hostNameComparisonMode="StrongWildcard"

                   maxBufferSize="1024000"maxBufferPoolSize="1024000"maxReceivedMessageSize="1024000"

                   messageEncoding="Text" textEncoding="utf-8"transferMode="Buffered"

                   useDefault W e b P r o x y ="true">

                   <readerQuotas maxDepth="32"maxStringContentLength="8192" maxArrayLength="1024000"

                       maxBytesPerRead="4096" maxNameTableCharCount="16384"/>

                   <security mode="None">

                        <transportclientCredentialType="None" proxyCredentialType="None"

                           realm=""/>

                        <messageclientCredentialType="UserName" algorithmSuite="Default"/>

                   </security>

               </binding>

           </basicHttpBinding>

          <!--

            <customBinding>

               <bindingname="CalculateServiceSoap12Binding">

                    <textMessageEncodingmaxReadPoolSize="64" maxWritePoolSize="16"

                       messageVersion="Soap12" writeEncoding="utf-8">

                        <readerQuotas maxDepth="32"maxStringContentLength="8192" maxArrayLength="16384"

                           maxBytesPerRead="4096" maxNameTableCharCount="16384"/>

                   </textMessageEncoding>

                    <httpTransportmanualAddressing="false" maxBufferPoolSize="524288"

                       maxReceivedMessageSize="65536" allowCookies="false"authenticationScheme="Anonymous"

                       bypassProxyOnLocal="false"decompressionEnabled="true"hostNameComparisonMode="StrongWildcard"

                        keepAliveEnabled="true"maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"

                        realm=""transferMode="Buffered"unsafeConnectionNtlmAuthentication="false"

                       useDefault W e b P r o x y ="true" />

                </binding>

            </customBinding>

            -->

       </bindings>

       <client>

           <endpoint address="http://localhost:4585/AfisService.asmx"binding="basicHttpBinding"

               bindingConfiguration="AfisServiceSoap"contract="GAWebService.AfisServiceSoap"

               name="AfisServiceSoap" />

           <endpointaddress="http://localhost:8080/AfisWebService/services/CalculateService.CalculateServiceHttpSoap11Endpoint/"

               binding="basicHttpBinding"bindingConfiguration="CalculateServiceSoap11Binding"

               contract="JavaWebService.CalculateServicePortType"name="CalculateServiceHttpSoap11Endpoint" />

          <!--

            <endpointaddress="http://localhost:8080/AfisWebService/services/CalculateService.CalculateServiceHttpSoap12Endpoint/"

               binding="customBinding"bindingConfiguration="CalculateServiceSoap12Binding"

               contract="JavaWebService.CalculateServicePortType"name="CalculateServiceHttpSoap12Endpoint" />

                -->

       </client>

 

   </system.serviceModel>

</configuration>

如上,去掉关于“http://localhost:8080/AfisWebService/services/CalculateService.CalculateServiceHttpSoap12Endpoint/”的定义,只保留CalculateServiceHttpSoap11Endpoint的定义即可。

另外,如果上传下载的文件超过65535个字节,还需要修改相应的参数,如上面的红色加粗的字体所示。

posted @ 2013-07-01 17:17  therockthe  阅读(798)  评论(1)    收藏  举报