需求:做這個項目之前需要用到一個java的webservice接口,首先想到的第一種方法就是添加服務引用了.但是這樣的話會有一個config文件 生成的dll必須要讀取config文件,config文件放到同一目錄也不行的,需要放到另一個工具的節點中才能被另外一個工具讀取,不幸的是另外一個工具不能添加節點 否者會boom
二:接下來就是只能手寫地址寫進去程序里面.然后在百度搜索了一下文章.代碼不出意外的粘貼復制,然后自己寫個c#的webserive測試完全沒問題..發現給java用時候就不行了.百思不得其解,報錯找不到命名空間之類的,那就不折騰了.
三:wcf提供的吧. 首先需要一個工廠方法和一個代理類.代理類可以用wcf工具生成,具體步驟如下http://blog.163.com/ningbao911@126/blog/static/3743053820132133616282/引用別人寫的吧 照着做就行這是原地址.這個時候就拿到了一個代理類,然后找一個工廠的方法
public static T CreateServiceByUrl<T>(string url) { return CreateServiceByUrl<T>(url, "basicHttpBinding"); } public static T CreateServiceByUrl<T>(string url, string bing) { try { if (string.IsNullOrEmpty(url)) throw new NotSupportedException("This url is not Null or Empty!"); EndpointAddress address = new EndpointAddress(url); Binding binding = CreateBinding(bing); ChannelFactory<T> factory = new ChannelFactory<T>(binding, address); return factory.CreateChannel(); } catch (Exception ex) { throw new Exception("創建服務工廠出現異常."); } } #endregion #region 創建傳輸協議 /// <summary> /// 創建傳輸協議 /// </summary> /// <param name="binding">傳輸協議名稱</param> /// <returns></returns> private static Binding CreateBinding(string binding) { Binding bindinginstance = null; if (binding.ToLower() == "basichttpbinding") { BasicHttpBinding ws = new BasicHttpBinding(); ws.MaxBufferSize = 2147483647; ws.MaxBufferPoolSize = 2147483647; ws.MaxReceivedMessageSize = 2147483647; ws.CloseTimeout = new TimeSpan(0, 30, 0); ws.OpenTimeout = new TimeSpan(0, 30, 0); ws.ReceiveTimeout = new TimeSpan(0, 30, 0); ws.SendTimeout = new TimeSpan(0, 30, 0); bindinginstance = ws; } else if (binding.ToLower() == "nettcpbinding") { NetTcpBinding ws = new NetTcpBinding(); ws.MaxReceivedMessageSize = 65535000; ws.Security.Mode = SecurityMode.None; bindinginstance = ws; } else if (binding.ToLower() == "wshttpbinding") { WSHttpBinding ws = new WSHttpBinding(SecurityMode.None); ws.MaxReceivedMessageSize = 65535000; ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows; ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows; bindinginstance = ws; } return bindinginstance; }
然后下面就是使用的代碼了
string url = "這里填寫你的接口地址";
客戶端生成的接口名 例如public interface AA就這樣寫
AA a=WcfInvokeFactory.CreateServiceByUrl<AA>(url);
a.方法(new 代理類里面需要調用傳參的方法("參數"));
