最近做一个 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 整理的更多相关文章

  1. Java客户端调用.NET的WebService

    项目需要去调用.NET的WebSrevice,本身是Java,研究了半天,终于有些头绪,记下来. 1,新建.NET WebService.只在原方法上加上一个string类型的参数str [WebMe ...

  2. java客户端调用c#的webservice服务

    此处使用到了CXF框架,可以使用以下坐标从maven仓库中获取相关jar包: <dependency> <groupId>org.apache.cxf</groupId& ...

  3. Java动态调用webService,axis2动态调用webService

    Java动态调用webService axis2动态调用webService >>>>>>>>>>>>>>>& ...

  4. java调用第三方的webservice应用实例

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示. 一些常用的webservice网站的链接地址: ...

  5. java调用第三方的webservice应用实例【转载】

    互联网上面有很多的免费webService服务,我们可以调用这些免费的WebService服务,将一些其他网站的内容信息集成到我们的Web应用中显示. 一些常用的webservice网站的链接地址: ...

  6. java调用.net的webservice接口

    要调用webservice,首先得有接口,用已经写好的接口地址在myEclipse的目标project中,右键->new web service client-> 选择JAX-WS方式,点 ...

  7. java中调用dll文件的两种方法

    一中是用JNA方法,另外是用JNative方法,两种都是转载来的, JNA地址:http://blog.csdn.net/shendl/article/details/3589676   JNativ ...

  8. JAX-WS 学习一:基于java的最简单的WebService服务

    JAVA 1.6 之后,自带的JAX-WS API,这使得我们可以很方便的开发一个基于Java的WebService服务. 基于JAVA的WebService 服务 1.创建服务端WebService ...

  9. cxf调用c#的webservice

    java调用c#的webservice,如今已经測试通过.并且用到了项目中. 如今把实现方式和遇到的问题分享给大家.详细源代码例如以下: JaxWsDynamicClientFactory dcf = ...

随机推荐

  1. 非均匀B样条拟合MATLAB程序

    直接上代码,多的不再说了. %------------------非均匀B样条拟合MATLAB程序----------------- clear k=; x=load('data.txt'); [n, ...

  2. java jvm学习笔记十(策略和保护域)

    欢迎转载请说明出处:http://blog.csdn.net/yfqnihao/article/details/8271415 前面一节,我们做了一个简单的实验,来说明什么是策略文件,在文章的最后,也 ...

  3. winform中设置FormBorderStyle为None后点击任务栏自动最小化实现

    在winform编程中,有时候我们可能对窗体样式需要定义,不适用系统自带的样式,这样我们可以设置FormBorderStyle属性为None.但是设置了FormBorderStyle为None后,我们 ...

  4. [GRYZ2015]足球联赛

    问题描述 巴蜀中学新一季的足球联赛开幕了.足球联赛有n 只球队参赛,每赛季,每只球队要与其他球队各赛两场,主客各一场,赢一场得3 分,输一场不得分,平局两只队伍各得一分.英勇无畏的小鸿是机房的主力前锋 ...

  5. NOIP2003 传染病控制

    题四     传染病控制 [问题背景] 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国 大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完 全认 ...

  6. codeforce 609A - USB Flash Drives

    排序水题 #include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm&g ...

  7. 使用Genymotion作Android开发模拟器:安装Genymotion、部署Genymotion Vitrue Device、安装Genymotion eclipse插件

    偶然听说Genymotion Android模拟器非常强大,到网上了解一番后,决定从AVD又慢又卡中解脱出来,折腾了半天终于部署好了,体验了一下,果然启动快,运行流畅,现在总结一下经验教训,供大家参考 ...

  8. AVLTree的节点删除

    当年实现自己的共享内存模板的时候,map和set的没有实现,本来考虑用一个AVLTree作为底层实现的,为啥,因为我当时的数据结构知识里面我和RBTree不熟,只搞过AVLTree,但当时我一直没有看 ...

  9. Guide to make CentOS 7 the perfect desktop

    原文地址:  http://www.dedoimedo.com/computers/fedora-pimp.html My original review of CentOS 7 was less e ...

  10. JAVA中关于Map的九大问题

    通常来说,Map是一个由键值对组成的数据结构,且在集合中每个键是唯一的.下面就以K和V来代表键和值,来说明一下java中关于Map的九大问题. 0.将Map转换为List类型 在java中Map接口提 ...