// C#
JavaService.Bean1 proxy = new JavaService.Bean1();
TextBox1.Text = proxy.getSample().ToString();
✅ 直接運行成功,Axis 預設編碼與 .NET 序列化器相容度高。
[WebService(Namespace = "https://www.my.com/SU")]
public class SumService : WebService {
[WebMethod]
[SoapRpcMethod(Action = "https://www.my.com/Rpc",
RequestNamespace = "https://www.my.com/SU",
ResponseNamespace = "https://www.my.com/SU")]
public int IntAdd(int a, int b) {
return a + b;
}
}
關鍵:`[SoapRpcMethod]` 明確指定 `Action`、`Request/ResponseNamespace`,否則 Java Axis 預設 SOAP Action 不匹配拋異常。
public class TestDotNetService {
public static void main(String[] args) {
try {
Integer a = 1, b = 2;
String endpoint = "https://localhost/MyServices/WebServiceTest/SumService.asmx";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
// 必須與 .NET 端 SoapRpcMethod 完全一致
call.setOperationName(new QName("https://www.my.com/SU", "IntAdd"));
call.addParameter("a", XMLType.XSD_INT, ParameterMode.IN);
call.addParameter("b", XMLType.XSD_INT, ParameterMode.IN);
call.setReturnType(XMLType.XSD_INT);
call.setUseSOAPAction(true);
call.setSOAPActionURI("https://www.my.com/Rpc"); // 對應 Action
Integer result = (Integer) call.invoke(new Object[]{a, b});
System.out.println("Result: " + result); // 輸出 3
} catch (Exception e) {
e.printStackTrace();
}
}
}
| 問題 | 原因 | 解決 |
|---|---|---|
| SoapAction 不匹配 | .NET 預設 Action = `Namespace/Method`,Axis 預設不同 | 雙方顯式指定相同 `SOAPAction` URI |
| 命名空間衝突 | Request/Response Namespace 不一致 | `[SoapRpcMethod]` 統一指定 |
| 資料類型不匹配 | Axis `XSD_DATE` vs .NET `int` | 參數類型務必對應 (`XSD_INT`) |
| 複雜類型 (DataSet) | .NET DataSet 序列化極其複雜 | 避免跨平台傳 DataSet,改用 XML 字串或 POCO/DTO |