asp.NetプロキシサーバからWebServiceを呼び出す
11241 ワード
protected void Button1_Click1(object sender, EventArgs e)
{
string[] args = new string[3];
args[0] = "*";
args[1] = "city";
args[2] = "1=1";
//@namespace = "System.Web.Services.WebService";
object obj = InvokeWebService("http://220.113.1.92/RecWeb/WebInterFace.asmx", "", "WebInterFace", "GetBinaryFormatData", args);
WebServiceSimple.ServiceReference1.WebInterFaceSoap wi = new WebServiceSimple.ServiceReference1.WebInterFaceSoapClient();
WebServiceSimple.ServiceReference1.GetBinaryFormatDataRequest gbfdRequest = new WebServiceSimple.ServiceReference1.GetBinaryFormatDataRequest("*", "city", "1=1");
//WebServiceSimple.WebReference.WebInterFace wi = new WebServiceSimple.WebReference.WebInterFace();
WebServiceSimple.ServiceReference1.GetBinaryFormatDataResponse response = wi.GetBinaryFormatData(gbfdRequest);
byte[] _byte = (byte[])obj;
DataSet ds = RetrieveDataSet(_byte);
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}
/// <summary>
/// dataset
/// </summary>
/// <param name="binaryData"></param>
/// <returns></returns>
public DataSet RetrieveDataSet(byte[] binaryData)
{
DataSet dataSetResult = null;
MemoryStream memStream = new MemoryStream(binaryData);
IFormatter brFormatter = new BinaryFormatter();
object obj = brFormatter.Deserialize(memStream);
dataSetResult = (DataSet)obj;
return dataSetResult;
}
public static object InvokeWebService(string url, string @namespace, string classname, string methodname, object[] args)
{
try
{
// WSDL
WebClient wc = new WebClient();
//
WebProxy proxy = new WebProxy("192.168.102.3:80", true);
System.Net.ServicePointManager.Expect100Continue = false;
proxy.Credentials = new System.Net.NetworkCredential("HRconsultant", "HRproject2010", "hpc.hafele.corp");
proxy.UseDefaultCredentials = true;
wc.Proxy = proxy;
System.Net.ServicePointManager.Expect100Continue = false;
Stream stream = wc.OpenRead(url + "?WSDL");
ServiceDescription sd = 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 csc = new CSharpCodeProvider();
ICodeCompiler icc = csc.CreateCompiler();
//
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);
object obj = Activator.CreateInstance(t);
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));
}
}