读书人

Java如何调用 .net的webService (附w

发布时间: 2012-08-26 16:48:06 作者: rapoo

求救Java怎么调用 .net的webService (附wsdl xml)
求救Java怎么调用 .net得webService (附wsdl xml)

请问怎么解析这个wsdl

用xfire环境

<?xml version="1.0" encoding="utf-8"?><wsdl:definitions name="BasePlatformProvider" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"><wsp:Policy wsu:Id="WSHttpBinding_IBasePlatformProvider_policy"><wsp:ExactlyOne><wsp:All><wsrm:RMAssertion xmlns:wsrm="http://schemas.xmlsoap.org/ws/2005/02/rm/policy"><wsrm:InactivityTimeout Milliseconds="600000"/><wsrm:AcknowledgementInterval Milliseconds="200"/></wsrm:RMAssertion><wsaw:UsingAddressing/></wsp:All></wsp:ExactlyOne></wsp:Policy><wsdl:types><xsd:schema targetNamespace="http://tempuri.org/Imports"><xsd:import schemaLocation="http://service.baoan.edu.cn/BasePlatformServices.svc?xsd=xsd4" namespace="http://tempuri.org/"/><xsd:import schemaLocation="http://service.baoan.edu.cn/BasePlatformServices.svc?xsd=xsd0" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/><xsd:import schemaLocation="http://service.baoan.edu.cn/BasePlatformServices.svc?xsd=xsd1" namespace="http://schemas.datacontract.org/2004/07/TYKYBOS.Persistence"/><xsd:import schemaLocation="http://service.baoan.edu.cn/BasePlatformServices.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/TYKYBOS.Model.FilterEntity"/><xsd:import schemaLocation="http://service.baoan.edu.cn/BasePlatformServices.svc?xsd=xsd3" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/><xsd:import schemaLocation="http://service.baoan.edu.cn/BasePlatformServices.svc?xsd=xsd5" namespace="http://schemas.datacontract.org/2004/07/TYKYBOS.Service"/><xsd:import schemaLocation="http://service.baoan.edu.cn/BasePlatformServices.svc?xsd=xsd6" namespace="http://schemas.datacontract.org/2004/07/TYKYBOS.Helpers"/></xsd:schema></wsdl:types>

<wsdl:message name="IBasePlatformProvider_GetOrgByLoginName_InputMessage"><wsdl:part name="parameters" element="tns:GetOrgByLoginName"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetOrgByLoginName_OutputMessage"><wsdl:part name="parameters" element="tns:GetOrgByLoginNameResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_IsLogin_InputMessage"><wsdl:part name="parameters" element="tns:IsLogin"/></wsdl:message><wsdl:message name="IBasePlatformProvider_IsLogin_OutputMessage"><wsdl:part name="parameters" element="tns:IsLoginResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetDeskBoardsBySearch_InputMessage"><wsdl:part name="parameters" element="tns:GetDeskBoardsBySearch"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetDeskBoardsBySearch_OutputMessage"><wsdl:part name="parameters" element="tns:GetDeskBoardsBySearchResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetUsersBySearch_InputMessage"><wsdl:part name="parameters" element="tns:GetUsersBySearch"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetUsersBySearch_OutputMessage"><wsdl:part name="parameters" element="tns:GetUsersBySearchResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetRolesBySearch_InputMessage"><wsdl:part name="parameters" element="tns:GetRolesBySearch"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetRolesBySearch_OutputMessage"><wsdl:part name="parameters" element="tns:GetRolesBySearchResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetMenusBySearch_InputMessage"><wsdl:part name="parameters" element="tns:GetMenusBySearch"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetMenusBySearch_OutputMessage"><wsdl:part name="parameters" element="tns:GetMenusBySearchResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetOrganizationBySearch_InputMessage"><wsdl:part name="parameters" element="tns:GetOrganizationBySearch"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetOrganizationBySearch_OutputMessage"><wsdl:part name="parameters" element="tns:GetOrganizationBySearchResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_SetDeskBoardDataSource_InputMessage"><wsdl:part name="parameters" element="tns:SetDeskBoardDataSource"/></wsdl:message><wsdl:message name="IBasePlatformProvider_SetDeskBoardDataSource_OutputMessage"><wsdl:part name="parameters" element="tns:SetDeskBoardDataSourceResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_PostNotify_InputMessage"><wsdl:part name="parameters" element="tns:PostNotify"/></wsdl:message><wsdl:message name="IBasePlatformProvider_PostNotify_OutputMessage"><wsdl:part name="parameters" element="tns:PostNotifyResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_PostNotice_InputMessage"><wsdl:part name="parameters" element="tns:PostNotice"/></wsdl:message><wsdl:message name="IBasePlatformProvider_PostNotice_OutputMessage"><wsdl:part name="parameters" element="tns:PostNoticeResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetDirectorys_InputMessage"><wsdl:part name="parameters" element="tns:GetDirectorys"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetDirectorys_OutputMessage"><wsdl:part name="parameters" element="tns:GetDirectorysResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetFiles_InputMessage"><wsdl:part name="parameters" element="tns:GetFiles"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetFiles_OutputMessage"><wsdl:part name="parameters" element="tns:GetFilesResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetFileContent_InputMessage"><wsdl:part name="parameters" element="tns:GetFileContent"/></wsdl:message><wsdl:message name="IBasePlatformProvider_GetFileContent_OutputMessage"><wsdl:part name="parameters" element="tns:_CreateDirectory_InputMessage"><wsdl:part name="parameters" element="tns:CreateDGetFileContentResponse"/></wsdl:message><wsdl:message name="IBasePlatformProviderirectory"/></wsdl:message><wsdl:message name="IBasePlatformProvider_CreateDirectory_OutputMessage"><wsdl:part name="parameters" element="tns:CreateDirectoryResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_CreateSystemFile_InputMessage"><wsdl:part name="parameters" element="tns:CreateSystemFile"/></wsdl:message><wsdl:message name="IBasePlatformProvider_CreateSystemFile_OutputMessage"><wsdl:part name="parameters" element="tns:CreateSystemFileResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_UpdateUserDetail_InputMessage"><wsdl:part name="parameters" element="tns:UpdateUserDetail"/></wsdl:message><wsdl:message name="IBasePlatformProvider_UpdateUserDetail_OutputMessage"><wsdl:part name="parameters" element="tns:UpdateUserDetailResponse"/></wsdl:message><wsdl:message name="IBasePlatformProvider_DeleteUserDetail_InputMessage"><wsdl:part name="parameters" element="tns:DeleteUserDetail"/></wsdl:message><wsdl:message name="IBasePlatformProvider_DeleteUserDetail_OutputMessage"><wsdl:part name="parameters" element="tns:DeleteUserDetailResponse"/></wsdl:message><wsdl:portType name="IBasePlatformProvider"><wsdl:operation name="GetOrgByLoginName"><wsdl:input wsaw:Action="http://tempuri.org/IBasePlatformProvider/GetOrgByLoginName" message="tns:IBasePlatformProvider_GetOrgByLoginName_InputMessage"/><wsdl:output wsaw:Action="http://tempuri.org/IBasePlatformProvider/GetOrgByLoginNameResponse" message="tns:IBasePlatformProvider_GetOrgByLoginName_OutputMessage"/></wsdl:operation><wsdl:operation name="IsLogin"><wsdl:input wsaw:Action="http://tempuri.org/IBasePlatformProvider/IsLogin" message="tns:IBasePlatformProvider_IsLogin_InputMessage"/>





[解决办法]
出自:http://www.cnblogs.com/nect/archive/2007/08/16/932633.html

最开始.net的Service代码如下(黄色背景是自己增加的代码):

//<%@ WebService Language="C#" Class="Service" Debug=true %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace Service
{
[WebService(Namespace="http://192.168.168.180/ss/Service.asmx")]

/// <summary>
/// Service1 的摘要说明。
/// </summary>
public class Service : System.Web.Services.WebService
{
public Service()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}

#region 组件设计器生成的代码

//Web 服务设计器所必需的
private IContainer components = null;

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{

}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

[WebMethod(Description="test")]
public string GetTestQuestions(string TeacherName,string Subject)
{
return "11111";
}
}
}
=================

Java调用代码:

public static String GetTestQuestions(String TeacherName,String Subject){
String result = "";
try{
Service service = new Service();
Call call = (Call) service.createCall();
call.setOperationName(new QName("", "GetTestQuestions"));
call.addParameter("TeacherName", XMLType.XSD_STRING, ParameterMode.IN);
call.addParameter("Subject", XMLType.XSD_STRING, ParameterMode.IN);
call.setTargetEndpointAddress(new URL(
"http://192.168.168.180/ss/Service.asmx"));
result = (String) call.invoke(new Object[] { TeacherName, Subject});

}catch(Exception e){
e.printStackTrace();
}

return result;
}
public static void main(String args[]){
System.out.println(UserWebService.GetTestQuestions("aaa", "HOMA060E"));

}
--------------------

结果用java调用时总提示:faultString: 服务器未能识别 HTTP 标头 SOAPAction 的值:。
--------------------

上网找了解决方法,最好修改的结果如下:

.net WebService代码:

在webservicenamespace后面增加:

[SoapDocumentService(RoutingStyle=SoapServiceRoutingStyle.RequestElement)]

java调用错误变成了:faultString: 无法识别请求元素 <GetTestQuestions xmlns=''>。

崩溃了……

……

经过做java同学的不懈努力,终于找到解决方法:

java绿色背景代码更改成:

call.setOperationName(new QName("http://192.168.168.180/ss/Service.asmx", "GetTestQuestions"));
终于得到期待的结果了。

总结一下原因:

.net的webservice指定了namespace:http://192.168.168.180/ss/Service.asmx,但是java调用时没有指定,所以总提示找不到“<GetTestQuestions xmlns=''>”,如果我们仔细查看.net webservice的soap请求格式时会发现,要求的格式是(注意蓝色背景的文字)


SOAP

下面是一个 SOAP 请求和响应示例。所显示的占位符需要由实际值替换。
POST /ss/service.asmx HTTP/1.1
Host: 192.168.168.180
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://192.168.168.180/ss/Service.asmx/GetTestQuestions"

<?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>
<GetTestQuestions xmlns="http://192.168.168.180/ss/Service.asmx">
<TeacherName>string</TeacherName>
<Subject>string</Subject>
</GetTestQuestions>
</soap:Body>
</soap:Envelope>

读书人网 >J2EE开发

热点推荐