Java调用.net的WebService

samool 发布暂无评论11013 阅读

AI摘要

文章介绍了如何实现Java调用.net的WebService。首先,提供了.net的WebService代码,包括命名空间、类定义和WebMethod。然后,展示了Java调用代码,包括创建Service对象、设置操作名称、添加参数和设置目标端点地址等步骤。最后,通过main方法测试了Java调用.net的WebService。

这几天公司需要做一个java和.net项目的整合,其中.net做了一个WebService,需要java来调用。本以为很容易的一个东西,结果弄了几天才弄好。黄色背景是自己增加的代码,特别注意,别问我为什么要这样用,因为我对.NET和JAVA都是半壶水,也不是很会,参照了网上很多其它方法,下面的方法完全调试成功,大家也可以试一下。

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("http://192.168.168.180/ss/Service.asmx", "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"));

 }

  1. 01Struts+Hibernate+Spring2017-07-31
  2. 02android 使用map应用:遍历一个java Map 2011-07-08
  3. 03SaMDeditor V2.4 更新内容2026-12-02
  4. 04恭喜《轻武爱学习》小程序备案成功!2026-11-24
  5. 05SaView 图阅 1.1.1 更新日志2026-11-18
  6. 06Book2EPUB 1.0.4 更新日志2026-11-18
  7. 07SaView 图阅 一款轻便纯粹的看图工具2026-11-16
  8. 08免费工具Book2EPUB:轻松将电子书转换为 EPUB2026-11-11
  9. 09SaBlog2026 主题功能介绍(v1.1.36)2026-10-26
  10. 10PHP2Shell 重大安全漏洞事件深度分析报告2026-10-23
COMMENTS / 一纸留言

读者留言 0 条

一纸留言,便是山水间的一次相逢。

还没有留言,欢迎写下第一段交流。

WRITE A COMMENT

写下你的留言