动态调用Webservice 支持Soapheader身份验证(转)
封装的WebserviceHelp类:
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Web.Services.Description;
using Microsoft.CSharp;
using System.Xml.Serialization; namespace MyServiceLibrary
{
/// <summary>
/// 动态调用WebService(支持SaopHeader)
/// </summary>
public class WebServiceHelper
{
/// <summary>
/// 获取WebService的类名
/// </summary>
/// <param name="wsUrl">WebService地址</param>
/// <returns>返回WebService的类名</returns>
private static string GetWsClassName(string wsUrl)
{
string[] parts = wsUrl.Split('/');
string[] pps = parts[parts.Length - ].Split('.');
return pps[];
} /// <summary>
/// 调用WebService(不带SoapHeader)
/// </summary>
/// <param name="wsUrl">WebService地址</param>
/// <param name="methodName">方法名称</param>
/// <param name="args">参数列表</param>
/// <returns>返回调用结果</returns>
public static object InvokeWebService(string wsUrl, string methodName, object[] args)
{
return InvokeWebService(wsUrl, null, methodName, null, args);
} /// <summary>
/// 调用WebService(带SoapHeader)
/// </summary>
/// <param name="wsUrl">WebService地址</param>
/// <param name="methodName">方法名称</param>
/// <param name="soapHeader">SOAP头</param>
/// <param name="args">参数列表</param>
/// <returns>返回调用结果</returns>
public static object InvokeWebService(string wsUrl, string methodName, SoapHeader soapHeader, object[] args)
{
return InvokeWebService(wsUrl, null, methodName, soapHeader, args);
} /// <summary>
/// 调用WebService
/// </summary>
/// <param name="wsUrl">WebService地址</param>
/// <param name="className">类名</param>
/// <param name="methodName">方法名称</param>
/// <param name="soapHeader">SOAP头</param>
/// <param name="args">参数列表</param>
/// <returns>返回调用结果</returns>
public static object InvokeWebService(string wsUrl, string className, string methodName, SoapHeader soapHeader, object[] args)
{
string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
if ((className == null) || (className == ""))
{
className = GetWsClassName(wsUrl);
}
try
{
//获取WSDL
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(wsUrl + "?wsdl");
ServiceDescription sd = ServiceDescription.Read(stream); //配置代理ServiceDescriptionImporter数据
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
//sdi.ProtocolName = "Soap";
//sdi.Style = ServiceDescriptionImportStyle.Server;
//sdi.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
sdi.AddServiceDescription(sd, null, null);
CodeNamespace cn = new CodeNamespace(@namespace); //生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu); //CSharpCodeProvider csc = new CSharpCodeProvider();
///ICodeCompiler icc = csc.CreateCompiler();
CodeDomProvider icc = CodeDomProvider.CreateProvider("CSharp"); //设定编译参数
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)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
} //生成代理实例,并调用方法
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace + "." + className, true, true); FieldInfo[] arry = t.GetFields(); FieldInfo client = null;
object clientkey = null;
if (soapHeader != null)
{
//Soap头开始
client = t.GetField(soapHeader.ClassName + "Value"); //获取客户端验证对象
Type typeClient = assembly.GetType(@namespace + "." + soapHeader.ClassName); //为验证对象赋值
clientkey = Activator.CreateInstance(typeClient); foreach (KeyValuePair<string, object> property in soapHeader.Properties)
{
typeClient.GetField(property.Key).SetValue(clientkey, property.Value);
}
//Soap头结束
} //实例类型对象
object obj = Activator.CreateInstance(t); if (soapHeader != null)
{
//设置Soap头
client.SetValue(obj, clientkey);
} System.Reflection.MethodInfo mi = t.GetMethod(methodName); return mi.Invoke(obj, args);
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
}
} /// <summary>
/// SOAP头
/// </summary>
public class SoapHeader
{
/// <summary>
/// 构造一个SOAP头
/// </summary>
public SoapHeader()
{
this.Properties = new Dictionary<string, object>();
} /// <summary>
/// 构造一个SOAP头
/// </summary>
/// <param name="className">SOAP头的类名</param>
public SoapHeader(string className)
{
this.ClassName = className;
this.Properties = new Dictionary<string, object>();
} /// <summary>
/// 构造一个SOAP头
/// </summary>
/// <param name="className">SOAP头的类名</param>
/// <param name="properties">SOAP头的类属性名及属性值</param>
public SoapHeader(string className, Dictionary<string, object> properties)
{
this.ClassName = className;
this.Properties = properties;
} /// <summary>
/// SOAP头的类名
/// </summary>
public string ClassName { get; set; } /// <summary>
/// SOAP头的类属性名及属性值
/// </summary>
public Dictionary<string, object> Properties { get; set; } /// <summary>
/// 为SOAP头增加一个属性及值
/// </summary>
/// <param name="name">SOAP头的类属性名</param>
/// <param name="value">SOAP头的类属性值</param>
public void AddProperty(string name, object value)
{
if (this.Properties == null)
{
this.Properties = new Dictionary<string, object>();
}
Properties.Add(name, value);
}
}
} }
使用方法介绍:
如果soapheader是这种方式:
public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
{
private string userID = string.Empty;
private string userPW = string.Empty; public string UserId
{
get { return userID; }
set { userID = value; }
}
public string UserPW
{
get { return userPW; }
set { userPW = value; }
}
}
调用上面ServiceHelper方式如下:
WebServiceHelper.SoapHeader header = new WebServiceHelper.SoapHeader("MySoapHeader");
header.AddProperty("UserId", "admin");
header.AddProperty("UserPW", "admin");
object r = WebServiceHelper.InvokeWebService("http://localhost:9015/WebService1.asmx","HelloWorld2",header,null);
Console.WriteLine(r.ToString());
Console.ReadKey();
动态调用Webservice 支持Soapheader身份验证(转)的更多相关文章
- 调用WebService时加入身份验证,以拒绝未授权的访问
众所周知,WebService是为企业需求提供的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务.但在有些时候的某些应用服务不希望被未授权访问,那么此时我们可以一下几种 ...
- ANDROID调用webservice带soapheader验证
最近的一个项目中调用webservice接口,需要验证soapheader,现将解决方法记录如下:(网上资料出处太多,就不做引用,原作者如看到,如有必要添加请通知) 1.先看接口 POST /webs ...
- 动态调用webservice及WCF服务
动态调用web服务,该方法只针对Web service, WCF的服务不行,如果是WCF的就通过工具直接生产代理类,把代理类配置到调用的项目中,通过配置客户端的终结点动态的取实现: 通过Svcutil ...
- [转]Net 下采用GET/POST/SOAP方式动态调用WebService C#实现
本文转自:http://www.cnblogs.com/splendidme/archive/2011/10/05/2199501.html 一直以来,我们都为动态调用WebService方法而烦恼. ...
- .Net 下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) [轉]Redfox
一直以来,我都为动态调用WebService方法而烦恼.在.Net环境下,最常用的方法就是采用代理类来调用WebService,可以通过改变代理类的Url属性来实现动态调用,但当xmlns改变时就会出 ...
- Atitit 动态调用webservice与客户端代理方式调用
Atitit 动态调用webservice与客户端代理方式调用 方式1: 使用call.invoke 直接调用WSDL,缺点:麻烦,不推荐--特别是JAVA调用.NET的WS时,会有不少的问题需要解 ...
- 动态调用WebService(C#) (非常实用)
通常我们在程序中需要调用WebService时,都是通过“添加Web引用”,让VS.NET环境来为我们生成服务代理,然后调用对应的Web服务.这样是使工作简单了,但是却和提供Web服务的URL.方法名 ...
- 动态调用webservice(部分转载)
动态调用webservice,做个笔记: public class WSHelper { /// < summary> /// 动态调用web服务 /// < /summary> ...
- C# 动态调用webservice
最近项目中,用到动态调用webservice的内容,此处记录下来,留着以后COPY(我们只需要在XML,config文件,或者数据库中配置webservice连接地址和方法名即可使用): using ...
随机推荐
- ecshop后台导航修改教程说明
ecshop后台导航修改教程说明 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2014-06-25 需要操作的文件为: 1.修改admin\includes\in ...
- latin1
Latin1是ISO-8859-1的别名,有些环境下写作Latin-1.ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII ...
- Why The Golden Age Of Machine Learning is Just Beginning
Why The Golden Age Of Machine Learning is Just Beginning Even though the buzz around neural networks ...
- Practical Machine Learning For The Uninitiated
Practical Machine Learning For The Uninitiated Last fall when I took on ShippingEasy's machine learn ...
- --hdu 1231 最大连续子序列(动态规划)
AC code: #include<stdio.h> int a[100005]; int main(void) { int n,i; int sum,maxn,tem,s,e,flag; ...
- SQL2005删除复制数据库的发布与订阅的方法(转载)
SQL2005删除复制数据库的发布与订阅的方法 --在测试环境中恢复从正式数据库服务器 上备份下来的bak文件后,正式环境里数据库复制的发布.订阅也被带进来了,结果恢复的数据库无法更改表结构,直接删除 ...
- apache 配置多个虚拟主机
修改文件:httd.conf 文件地址:D:\wamp\bin\apache\Apache2.2.21\conf #配置虚拟主机<VirtualHost 127.0.0.3:80>Serv ...
- jQuery1.11源码分析(5)-----Sizzle编译和过滤阶段[原创]
在上一章中,我们说到在之前的查找阶段我们已经获得了待选集seed,那么这一章我们就来讲如何将seed待选集过滤,以获得我们最终要用的元素. 其实思路本质上还是不停地根据token过滤,但compile ...
- bootstrap tab切换如何让鼠标移动自动切换内容
bootstrap集成了很多功能,比如nav-tabs组件,可以将相似的内容集中在一个区块中展示.bootstrap tab切换默认是要点击才会切换的,如何实现鼠标移动就自动切换呢?如下图所示,光标移 ...
- editplus如何插入当前时间_Ctrl+D
之前的工作日志一般都是用excel来写的,但那个占用内存有点大,有时也比较麻烦,有时内容一行没办法显示,会自动截断,有点类似缩略图,无法一目了然 习惯了使用editplus,轻便快速,不占内存.但是有 ...