java控制檯調用webserivce(C#寫的)例子

步驟:

1、添加axis包

2、webservices中的代碼

     /// <summary>
    /// BarCodeServices 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的註釋。
    // [System.Web.Script.Services.ScriptService]
    public class BarCodeServices : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetBarCodeInformation(string BarCode, string UserName, string PassWord)
        {

           return "XXXXXXX";

        }

       }

3、Java中的代碼:

import java.net.URL;  
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode; 
import org.apache.axis.client.Call;  
import org.apache.axis.client.Service;  
import org.apache.axis.encoding.XMLType;
public class MainClass {

public static void main(String[] args)  throws Exception{
WebServicesUsed();
}
//webservices調用例子
public static void WebServicesUsed() throws Exception
{
// 定義方法  
String method = "GetBarCodeInformation";  
// 定義服務  
Service service = new Service();  
// 測試1:調用HelloWorld方法,方法沒有參數  
Call call2 = (Call) service.createCall();  
call2.setTargetEndpointAddress(new java.net.URL("http://10.2.32.64:7005/WebServices/BarCodeServices.asmx")); //weibservice地址
call2.setUseSOAPAction(true);  
call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));  
call2.setOperationName(new QName("http://tempuri.org/", method));  //webservice中namespace
call2.setSOAPActionURI("http://tempuri.org/GetBarCodeInformation");  //webservice中namespace的方法
call2.addParameter(new QName("http://tempuri.org/", "BarCode"),XMLType.XSD_STRING, ParameterMode.IN);  //參數
call2.addParameter(new QName("http://tempuri.org/", "UserName"),XMLType.XSD_STRING, ParameterMode.IN);   //參數
call2.addParameter(new QName("http://tempuri.org/", "PassWord"),XMLType.XSD_STRING, ParameterMode.IN);   //參數
String retVal2 = (String) call2.invoke(new Object[] { "8427650000468","SJYB","81e50f63" });  //參數值
System.out.println(retVal2);  
}

}

4、輸出結果(json格式):

{"barcode":"8427650000468"}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章