標籤:
需求:做這個項目之前需要用到一個java的webservice介面,首先想到的第一種方法就是加入服務參考了.但是這樣的話會有一個config檔案 產生的dll必須要讀取config檔案,config檔案放到同一目錄也不行的,需要放到另一個工具的節點中才能被另外一個工具讀取,不幸的是另外一個工具不能添加節點 否者會boom
二:接下來就是只能手寫地址寫進去程式裡面.然後在百度搜尋了一下文章.代碼不出意外的粘貼複製,然後自己寫個c#的webserive測試完全沒問題..發現給java用時候就不行了.百思不得其解,報錯找不到命名空間之類的,那就不折騰了.
三:wcf提供的吧. 首先需要一個Factory 方法和一個代理類.代理類可以用wcf工具產生,具體步驟如下http://blog.163.com/[email protected]/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;}View Code
然後下面就是使用的代碼了
string url = "這裡填寫你的介面地址";
用戶端產生的介面名 例如public interface AA就這樣寫
AA a=WcfInvokeFactory.CreateServiceByUrl<AA>(url);
a.方法(new 代理類裡面需要調用傳參的方法("參數"));
.net 調用外部的webservice介面