动态调用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 ...
随机推荐
- TFS 配置 报表权限问题
通过[项目门户网站]访问,却被提示ReportService权限不足时, 提示:处理报表时出错. (rsProcessingAborted) 对数据集 “dsiteration” 执行查询失败 同样 ...
- 锋利的jQuery-4--动画方法总结简表
- xss跨站攻击测试代码
'><script>alert(document.cookie)</script> ='><script>alert(document.cookie)& ...
- c语言 函数传输传递的三种方式(值、指针、引用)
本文摘自<彻底搞定c指针> 一.三道考题开讲之前,我先请你做三道题目.(嘿嘿,得先把你的头脑搞昏才行……唉呀,谁扔我鸡蛋?)考题一,程序代码如下:void Exchg1(int x, in ...
- Redis介绍以及安装(Linux与windows)
1.liunux系统 redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了memcached的 不足,它支持存储的value类型 ...
- powerdesigner设置表主键列为自动增长。
powerdesigner 版本12.5 创建表就不说了.下面开始介绍设置自动增长列. 1 在表视图的列上创建.双击表视图,打开table properties ———>columens ,双击 ...
- ctags 的最简单使用
vim插件ctags的安装和使用 2013-11-19 20:47 17109人阅读 评论(0) 收藏 举报 分类: 开发工具(3) linux编程(9) c/c++编程(11) 版权声明:本 ...
- linux下ping的C语言实现(转)
#include <stdio.h> #include <signal.h> #include <arpa/inet.h> #include <sys/typ ...
- BNUOJ 1037 精神控制
XsuagrX喜欢到处唬人,各种唬.这不,经过刻苦修炼,他终于掌握了Bane Element的Ultra绝技加强版,恶魔掌控(快捷键F)(YY中&……).当XsugarX对某个人胡言乱语Q@# ...
- Linux的watch命令 — 实时监测命令的运行结果
Linux的watch命令 — 实时监测命令的运行结果 watch 是一个非常实用的命令,基本所有的 Linux 发行版都带有这个小工具,如同名字一样,watch 可以帮你监测一个命令的运行结果,省得 ...