java 调用.net web service

这几天公司需要做一个java.net项目的整合,其中.net做了一个WebService,需要java来调用。本以为很容易的一个东西,结果弄了几天才弄好。

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

//<%@ 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: 无法识别请求元素 &lt;GetTestQuestions xmlns=''&gt;

崩溃了……

……

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

java绿色背景代码更改成:

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

总结一下原因:

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

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>

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java调用Web服务,并结合SofaMQ使用客户端和服务端的完整代码: #### 服务端代码 ```java import com.alipay.sofa.rpc.config.ProviderConfig; import com.alipay.sofa.rpc.config.ServerConfig; import com.example.webservice.HelloWorld; import com.example.webservice.impl.HelloWorldImpl; public class Server { public static void main(String[] args) { ServerConfig serverConfig = new ServerConfig() .setProtocol("bolt") .setPort(12200) .setDaemon(false); ProviderConfig<HelloWorld> providerConfig = new ProviderConfig<HelloWorld>() .setInterfaceId(HelloWorld.class.getName()) .setRef(new HelloWorldImpl()) .setServer(serverConfig); providerConfig.export(); } } ``` 这个示例代码假设你已经有一个名为"HelloWorld"的Web服务,它的实现类为"HelloWorldImpl"。你需要使用SofaRPC创建一个服务端,并将"HelloWorldImpl"暴露为Web服务。 #### 客户端代码 ```java import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.alipay.sofa.rpc.config.ConsumerConfig; import com.example.webservice.HelloWorld; public class Client { public static void main(String[] args) throws Exception { ConsumerConfig<HelloWorld> consumerConfig = new ConsumerConfig<HelloWorld>() .setInterfaceId(HelloWorld.class.getName()) .setProtocol("bolt") .setDirectUrl("bolt://localhost:12200"); HelloWorld hello = consumerConfig.refer(); System.out.println(hello.sayHello("World")); } } ``` 这个示例代码假设你已经有一个名为"HelloWorld"的Web服务,并且它已经被暴露在"http://localhost:8080/hello"上。你需要使用SofaRPC创建一个客户端,并将"HelloWorld"声明为一个消费者。接下来,你需要使用ConsumerConfig.refer()方法获取HelloWorld接口的实例,并调用它的方法。 你需要将"com.example.webservice.HelloWorld"替换为你的实际接口类。你还需要根据你的实际情况修改端口号和URL。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值