今天寫了乙個c#呼叫webservice公共類,供以後借鑑學習
public class commonwebservice
set
}#region invokewebservice
/// < summary>
/// 動態呼叫web服務
///< /summary>
/// < param name="url">wsdl服務位址< /param>
/// < param name="methodname">方法名< /param>
/// < param name="args">引數< /param>
/// < returns>< /returns>
public object invokewebservice(string url, string methodname, object args)
/// < summary>
/// 動態呼叫web服務
/// < /summary>
/// < param name="url">wsdl服務位址< /param>
/// < param name="classname">類名< /param>
/// < param name="methodname">方法名< /param>
/// < param name="args">引數< /param>
/// < returns>< /returns>
public object invokewebservice(string url, string classname, string methodname, object args)
try?", url, "wsdl");
}stream stream = wc.openread(url);
system.web.services.description.servicedescription sd = system.web.services.description.servicedescription.read(stream);
servicedescriptionimporter sdi = new servicedescriptionimporter();
sdi.addservicedescription(sd, "", "");
codenamespace cn = new codenamespace(@namespace);
//生成客戶端**類**
codecompileunit ccu = new codecompileunit();
ccu.namespaces.add(cn);
sdi.import(cn, ccu);
csharpcodeprovider icc = new csharpcodeprovider();
//設定編譯引數
compilerparameters cplist = new compilerparameters();
cplist.generateexecutable = false;
cplist.generateinmemory = true;
cplist.referencedassemblies.add("system.dll");
cplist.referencedassemblies.add("system.xml.dll");
cplist.referencedassemblies.add("system.web.services.dll");
cplist.referencedassemblies.add("system.data.dll");
//編譯**類
compilerresults cr = icc.compileassemblyfromdom(cplist, ccu);
if (true == cr.errors.haserrors)
throw new exception(sb.tostring());
}//生成**例項,並呼叫方法
system.reflection.assembly assembly = cr.compiledassembly;
type t = assembly.gettype(@namespace + "." + classname, true, true);
object obj = activator.createinstance(t);
system.reflection.methodinfo mi = t.getmethod(methodname);
//設定webservice超時時間
((system.web.services.protocols.webclientprotocol)(obj)).timeout = this.outtime;
return mi.invoke(obj, args);
/*
* propertyinfo propertyinfo = type.getproperty(propertyname);
* return propertyinfo.getvalue(obj, null);
* */
}catch (exception ex)
}/// /// 根據webserviceurl獲得類名稱
///
/// webserviceurl
/// 類名稱
public static string getwsclassname(string wsurl)
return pps[0];
}#endregion
/// /// json物件反序列化
///
///
///
///
public static t jsondeserializeio(string jsonstring)
}/// /// json物件序列化
///
///
///
///
//json序列化
public static string jsonserializerio(t t)
}}
C 動態呼叫WebService
使用示例 trycatch exception ex 類檔案 using system using system.web using system.xml using system.collections using system.net using system.text using system...
C 動態呼叫WebService
原文 c 動態呼叫webservice c 動態webservice呼叫 string public string wstest string suc string wsd.executequery name,str return suc c using system using system.co...
C 動態呼叫webservice
在做多個系統整合的時候,由於各系統廠商採用不同的架構,在專案實施前期,各業務對業務理解不夠深入,系統介面可能會有較多變化,在此背景下,動態呼叫webserivce就變得靈活了,降低了系統整合的耦合度。下面介紹動態呼叫的具體步驟 2.使用 servicedescription 建立和格式化 wsdl ...