Java调用C#webService接口

转载:https://blog.csdn.net/hu_zhiting/article/details/78755104
https://max.book118.com/html/2018/1228/7030144004001200.shtm

java调用C#webService接口,主要有三种方式:1.HttpClient 2.HttpURLConnection 3.插件生成客户端代码

生成客户端代码

用到的工具

MyEclipse 10

建立java客户端步骤

第一步 获取Webservice的URL,如:http://127.0.0.1/MyWebService.asmx?wsdl
第二部 再MyEclipse创建Web Service client 如图所示
①新建一个web
Java调用C#webService接口_第1张图片
Java调用C#webService接口_第2张图片
Java调用C#webService接口_第3张图片
Java调用C#webService接口_第4张图片
在WSDL URL输入你获取的C#Webservice地址
点击next 等到他测试连接完成(原文说要等到连接成功,否则就要检查URL是否正确并且在重新输入,但是我的这里一直会报错
【I: (BP2402) The wsdl:binding element does not use a soapbind:binding element as defined in section “3 SOAP Binding.” of the WSDL 1.1 specification】)点击完成,会在项目中生成java文件,这样就等于你的客户端已经自动生成完成了,现在就等着去调用service里的方法
调用Webservice方法
第一步 一般这里都点开和service名字相同的java文件
这个URl(http://127.0.0.1/MyWebService.asmx?wsdl)里面就包含了Webservice的名字所以我们点开MyWebService.java
第二部 写一个main方法调用

public static void main(String[] args){
MyWebService service = new MyWebService();
ArrayOfString myAry = service.getMyWebServiceSoap().returnList();
//调用list数组
ArrayIfInt myAry = service.getMyWebServiceSoap().returnIntGroup();//调用int数组
ArrayOfString myAry = service.getMyWebserviceSoap().returnStringGroup();//调用String数组
String s=service.getMyWebserviceSoap().hellowWorld();//调用helloworld方法
System。out。println(s);
Main m= new Main();
m.setUserName(“李四”);
String s= service.service.getMyWebserviceSoap().checkClass(m)
System.out.println(s);//把Main类传递给WebService
}

C#的WebService 程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.WbeServices;
using System.WebServices.Protocols;

namespace WebApplication1
{
///
///MyWebService的摘要说明
///
///
/*[SoapRpcMethod(Action = “http://192.168.0.101/”,
RequestNamespace = “http://192.168.1.101”],
ResponseNamespace = “http://192.168.1.101”)]

*/

[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinging(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[System.ComponentModel.ToolBoxItem(false)]
//若要允许使用ASP.NET AJAX 从脚本中调用此Web服务,请取消对下行的注释。
[System.WebScript.Services.ScriptService]
public class MyWebService:System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return"Hello World";
}
///
///返回list类型数据
///
///
[WebMethod]
public List returnList()
{
List listString = new List();
listString.Add(“1”);
listString.Add(“2”);
listString.Add(“3”);
listString.Add(“4”);
listString.Add(“5”);
return listString;
}
///
///返回int型一维数组
///
///
[WebMethod]
public int[] returnIntGroup()
{
int[] num = new int []{100,200,300,400,500};
return num;
}
///
///返回string型一维数组
///
///
[WebMethod]
public string[] returnStringGroup()
{
string[] str = new string[]{“1000”,“2000”,“3000”};
return str;
}
///
///返回string型二维数组
///
///
[WebMethod]
public string[][] returnStringArray()
{
int n = 2;
int m= 2;
string[][] str = new string[n][];
str[0] = new string[m];
str[0][0] = “张三”;
str[0][1] = “90”;
str[1] = new string[m];
str[1][0] = “李四”;
str[1][1]=“80”;
return str;
}
///
///返回一个类
///
///
[WebMethod]
public string checkClass(main mainClass)
{
string str = “失败”;
main user = (main)mainClass;
if(user.UserName==“李四”)
{
str=“成功”;
}
return str;
}
}``
}

类调用注意事项

这里值得提醒 在传递类的时候哦 C#和java 都必须要拥有共同的类才行。
比如C#有一个Main类 程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
public class main
{
private string userName;
public string UserName
{
get{return userName;}
set{userName=value;}
}
private string address;
public string Address
{
get{return address;}
set{address = value;}
}
private int age;
public int Age
{
get{return age;}
set{age = value;}
}
private string[] phone;
public string[] Phone
{
get{return phone;}
set{phone = value;}
}
}
}

java中也有一个Main类 这个类是在java 在创建客户端的时候自动创建的
程序如下:
public class Main{
@XmlElement(name = “UserName”)
Protected String userName;
@XmlElement(name = “Address”)
protected String address;
@XmlElement(name = “Age”)
protected int age;
@XmlElement(name = “Phone”)
protected ArrayOffString phone;
public String getUserName(){
return userName;
}
public void setUserName(String value){
this.userName = value;
}
public String getAddress(){
return address;
}
public void setAddress(String value){
this.address = value;
}
public int getAge(){
return age;
}
public void setAge(int value){
this.age = value;
}
public ArrayOfString getPhone(){
return phone;
}
public void serPhone(ArrayOfString value){
this.phone = value;
}
}

总结

在进行java 调用C#WebService的时候首先要确定service的URL地址正确,然后根据提示在java开发工具里自动生成相关的程序代码,免去了手动写代码的繁琐。在调用的时候需要自己研究每一个方法的作用。

我自己按照上面的教程做了一个helloworld的调用测试
C#接口代码:

    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "赵迪你真棒!!!";
        }
        [WebMethod(Description = "加法")]
        public int Add(int a, int b)
        { 
            return a+b;
        }
    }

java调用接口输出返回值
main方法写在WebService1.java中
代码如下:

public static void main(String[] args){
		WebService1 service = new WebService1();
		String s=service.getWebService1Soap().helloWorld();//调用helloworld方法
		 System.out.println(s);
	}

得到结果如图:
在这里插入图片描述
调用helloworld成功。

你可能感兴趣的:(Java调用C#webService接口)