Java 之 调用.Net的 WebService 整理

摘要:
最近,我做了一个java调用的项目。网络服务。其中,。net制作了一个web服务,需要java调用=null){components.Dispose();}基础处置;}#endregion〔WebMethod〕publicstringGetTestQuestions{return“1”;}}}}====Java调用代码:publicstaticStringGetTestQuestions{Stringresult=“”;try{Serviceservice=newService();Callcall=service.createCall();//call.setOperationName;call.setOperation Name;call.addParameter;call.setTargetEndpointAddress;result=call.invoke;}捕获{e.printStackTrace();}returnresult;}publicstaticvoidmain{System.out.println;}当在java中调用结果时,它总是提示:faultString:服务器无法识别HTTP标头SOAPAction:的值。
  最近做一个 java 调用 .net 服务的项目,其中 .net做了一个WebService,需要java来调用。

  最开始.net的Service代码如下:

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 "1";
        }
    }
}


=================

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("Namespace名", "方法名"));   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绿色背景代码更改成: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 请求和响应示例。所显示的占位符需要由实际值替换。

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>

免责声明:文章转载自《Java 之 调用.Net的 WebService 整理》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇WordPress引用静态资源方法CoreML 简单使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java自学-类和对象 传参

Java中的传参 变量有两种类型 基本类型 和类类型 参数也是变量,所以传参分为基本类型传参类类型传参 步骤 1 : 基本类型传参 基本类型传参在方法内,无法修改方法外的基本类型参数 public class Hero { String name; //姓名 float hp; //血量...

Kafka之SpringBoot集成Kafka实战

  在spring应用中如果需要订阅kafka消息,通常情况下我们不会直接使用kafka-client, 而是使用更方便的一层封装spring-kafka。  在spring-kafka在运行时会启动两类线程,一类是Consumer线程,另一类是Listener线程。前者用来直接调用kafka-client的poll()方法获取消息,后者才是调用我们代码中...

构建 Owin 中间件 来获取客户端IP地址

Not so long ago, we discussed on this blog the possible ways of retrieving the client’s IP address in ASP.NET Web API. With the latest changes in the Web API 2 infrastructure, and...

Android笔记之权限库AndPermission

GitHub地址:https://github.com/yanzhenjie/AndPermission 这个库可以节省不少代码量和时间 使用示例如下 findViewById(R.id.btnGetLocation).setOnClickListener(new View.OnClickListener() { @...

C#正则表达式合并连续空格为单个空格

第一种方法:       使用 System.Text.RegularExpressions.Regex.Replace()方法   string result = String.Empty;   string str = "Just     Test the  Method";   result = Regex.Replace(str, "\s{2,}"...

rokectMq

目录 消息模型 集群部署 NameServer producer消息队列选择器MessageQueueSeleetor 消息存储 commitLog数据存储文件 ConsumeQueue索引文件 IndexFile索引文件 消息查找 根据偏移量查询 根据消息id查询 根据消息key查询 实时更新消息消费队列与索引文件 消息队列与索引文件恢复 过期...