动态webservice调用接口
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Hishop.Plugins
{
/// <summary>
/// 利用WebRequest/WebResponse进行WebService调用的类
/// </summary>
public class WebServiceCaller
{
#region Tip:使用说明
//webServices 应该支持Get和Post调用,在web.config应该增加以下代码
//<webServices>
// <protocols>
// <add name="HttpGet"/>
// <add name="HttpPost"/>
// </protocols>
//</webServices> //调用示例:
//Hashtable ht = new Hashtable(); //Hashtable 为webservice所需要的参数集
//ht.Add("str", "test");
//ht.Add("b", "true");
//XmlDocument xx = WebSvcCaller.QuerySoapWebService("http://localhost:81/service.asmx", "HelloWorld", ht);
//MessageBox.Show(xx.OuterXml);
#endregion /// <summary>
/// 需要WebService支持Post调用
/// </summary>
public static XmlDocument QueryPostWebService(String URL, String MethodName, Hashtable Pars)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
SetWebRequest(request);
byte[] data = EncodePars(Pars);
WriteRequestData(request, data);
return ReadXmlResponse(request.GetResponse());
} /// <summary>
/// 需要WebService支持Get调用
/// </summary>
public static XmlDocument QueryGetWebService(String URL, String MethodName, Hashtable Pars)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + ParsToString(Pars));
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
SetWebRequest(request);
return ReadXmlResponse(request.GetResponse());
} /// <summary>
/// 通用WebService调用(Soap),参数Pars为String类型的参数名、参数值
/// </summary>
public static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars)
{
if (_xmlNamespaces.ContainsKey(URL))
{
return QuerySoapWebService(URL, MethodName, Pars, _xmlNamespaces[URL].ToString());
}
else
{
return QuerySoapWebService(URL, MethodName, Pars, GetNamespace(URL));
}
} private static XmlDocument QuerySoapWebService(String URL, String MethodName, Hashtable Pars, string XmlNs)
{
_xmlNamespaces[URL] = XmlNs;//加入缓存,提高效率
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\"");
SetWebRequest(request);
byte[] data = EncodeParsToSoap(Pars, XmlNs, MethodName);
WriteRequestData(request, data);
XmlDocument doc = new XmlDocument(), doc2 = new XmlDocument();
doc = ReadXmlResponse(request.GetResponse()); XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
String RetXml = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;
doc2.LoadXml("<root>" + RetXml + "</root>");
AddDelaration(doc2);
return doc2;
}
private static string GetNamespace(String URL)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "?WSDL");
SetWebRequest(request);
WebResponse response = request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sr.ReadToEnd());
sr.Close();
return doc.SelectSingleNode("//@targetNamespace").Value;
} private static byte[] EncodeParsToSoap(Hashtable Pars, String XmlNs, String MethodName)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"></soap:Envelope>");
AddDelaration(doc);
//XmlElement soapBody = doc.createElement_x_x("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
XmlElement soapBody = doc.CreateElement("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
//XmlElement soapMethod = doc.createElement_x_x(MethodName);
XmlElement soapMethod = doc.CreateElement(MethodName);
soapMethod.SetAttribute("xmlns", XmlNs);
foreach (string k in Pars.Keys)
{
//XmlElement soapPar = doc.createElement_x_x(k);
XmlElement soapPar = doc.CreateElement(k);
soapPar.InnerXml = ObjectToSoapXml(Pars[k]);
soapMethod.AppendChild(soapPar);
}
soapBody.AppendChild(soapMethod);
doc.DocumentElement.AppendChild(soapBody);
return Encoding.UTF8.GetBytes(doc.OuterXml);
}
private static string ObjectToSoapXml(object o)
{
XmlSerializer mySerializer = new XmlSerializer(o.GetType());
MemoryStream ms = new MemoryStream();
mySerializer.Serialize(ms, o);
XmlDocument doc = new XmlDocument();
doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
if (doc.DocumentElement != null)
{
return doc.DocumentElement.InnerXml;
}
else
{
return o.ToString();
}
} /// <summary>
/// 设置凭证与超时时间
/// </summary>
/// <param name="request"></param>
private static void SetWebRequest(HttpWebRequest request)
{
request.Credentials = CredentialCache.DefaultCredentials;
request.Timeout = ;
} private static void WriteRequestData(HttpWebRequest request, byte[] data)
{
request.ContentLength = data.Length;
Stream writer = request.GetRequestStream();
writer.Write(data, , data.Length);
writer.Close();
} private static byte[] EncodePars(Hashtable Pars)
{
return Encoding.UTF8.GetBytes(ParsToString(Pars));
} private static String ParsToString(Hashtable Pars)
{
StringBuilder sb = new StringBuilder();
foreach (string k in Pars.Keys)
{
if (sb.Length > )
{
sb.Append("&");
}
//sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString()));
}
return sb.ToString();
} private static XmlDocument ReadXmlResponse(WebResponse response)
{
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
String retXml = sr.ReadToEnd();
sr.Close();
XmlDocument doc = new XmlDocument();
doc.LoadXml(retXml);
return doc;
} private static void AddDelaration(XmlDocument doc)
{
XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(decl, doc.DocumentElement);
} private static Hashtable _xmlNamespaces = new Hashtable();//缓存xmlNamespace,避免重复调用GetNamespace
}
}
//调用并读取解析返回结果 DataSet ds = new DataSet();
XmlNode xmlNode1;
XmlDataDocument xd = new XmlDataDocument();
StringBuilder sb;
Hashtable ht = new Hashtable();
ht.Add("xmlIn", "<Request><MemCode>001</MemCode></Request>");
xmlNode1 = Hishop.Plugins.WebServiceCaller.QuerySoapWebService("http://xxx.xxxx.com/Service.asmx", "SinPointQuery", ht);
if (xmlNode1 == null)
{
return;
}
string xmlstr= HttpUtility.HtmlDecode(xmlNode1.OuterXml);
sb = new StringBuilder(xmlstr);
if (sb.ToString().Equals(""))
{
return;
}
xd.LoadXml(sb.ToString());
ds.ReadXml(new XmlNodeReader(xd));
//ds可以返回出结果集
动态webservice调用接口的更多相关文章
- C#动态webservice调用接口 (JAVA,C#)
C#动态webservice调用接口 using System; using System.Collections; using System.IO; using System.Net; using ...
- (转)C#动态webservice调用接口
原文地址:http://www.cnblogs.com/zouhao/p/6236785.html ,此处仅测试了使用Http post请求调用webservice 调用类: using Syst ...
- webservice调用接口,接口返回数组类型
1. 其中sendSyncMsg1接口是方法名,Vector实现了List接口,xml是sendSyncMsg1的方法形参 Service service = new Service(); Call ...
- 常用的WebService调用接口
天气预报Web服务,数据来源于中国气象局Endpoint :http://www.webxml.com.cn/WebServices/WeatherWebService.asmxDisco ...
- webService 客户端接口调用【java】
最近实际项目中使用到了WebService,简单总结下使用方式: 1.拿到接口:http://*******:8080/osms/services/OrderWebService?wsdl 我们可以将 ...
- 分享知识-快乐自己:HttpClient 访问 WebService 开放接口
HttpClient: 场景需求如下: 1.项目中需要与一个基于HTTP协议的第三方的接口进行对接 2.项目中需要动态的调用WebService服务(不生成本地源码) 3.项目中需要利用其它网站的相关 ...
- 使用自定义签名的https的ssl安全问题解决和metro的webservice调用
最近一直在忙新的项目,每天加班到8点多,都没来写博客了.新的项目遇到了很多问题,现在趁着突然停电来记录下调用https的问题吧. 我们服务主要是,我们调用数据源数据,并且再提供接口供外部数据调用. 我 ...
- webService调用的N种方式
一.服务器端代码 新建一个web Service Project工程(貌似普通的java工程也可以,这里不太明白各种webService的框架有什么区别),建立如下的类: package com.bo ...
- 动态WebService方法
[转] 调用Webservice的方法一般是通过右击项目-->添加服务引用-->输入Webservice地址-->前往-->确定,这样可以顺利调用服,但是需要注意的一点是:如果 ...
随机推荐
- JavaScript -- 清除缓存
在客户端有一个HTML文件,用来提交输入信息,问题在于:每次按刷新时,发觉并不是整个页面重新被装载,好似是缓存中. 因为文本框中仍出现上次输入的值,只有在地址栏中按回车整个页面才重新装载,应当怎样避免 ...
- 面经 | 我是如何通过校招拿到京东的Offer的
本文来自粉丝投稿,原作者:红鼻子熊. 版权归Hollis所有. OFFER:京东2018校招物流研发岗 个人:211小硕 面试时间:2017年秋天 整体:三轮面试,前两轮为技术面试,最后为hr面试 一 ...
- Codeforces Round #313 (Div. 1) Gerald's Hexagon
http://codeforces.com/contest/559/problem/A 题目大意:按顺序给出一个各内角均为120°的六边形的六条边长,求该六边形能分解成多少个边长为1的单位三角形. 解 ...
- python遍历目录的方法 walk listdir
这篇文章里描述了这些情况: https://www.cnblogs.com/jiaxin359/p/7324077.html 不用递归的时候,用 listdir 需要递归的时候,用walk
- http://my.oschina.net/China2012/blog/178655
http://my.oschina.net/China2012/blog/178655 http://git.oschina.net/huangyong/smart-framework
- HDU1505(HDU1506的加强版)
昨天打 CF又跪了.近期睡不好睡不好睡不好-感觉整个人都累傻了,根本无办法写下去,只写了一题签到题就跪了orz..从未试过这么悲剧. 今天早上凭着我的意念("怨念").七点又起来了 ...
- activemq两种实现方式
第一种:点对点 #发布者public class Producer { private static final String userName = ActiveMQXAConnectionFacto ...
- Textarea自动适用高度且无滚动条解决方案
今日的系统需要动态显示一项数据库里面的内容,该内容包含换行等格式字符,要求如实的反应在页面上. 最初解决办法是使用textarea控件,代码如下: <textarea style="b ...
- WIN7如何查找网络打印机
1 在开始菜单中输入"打印机"并点击"添加打印机" 2 点击下面一个,并搜索家庭组的打印机,一般可以搜到(注意这台电脑不能关机或睡眠). 3 查找并添加会需要安 ...
- Hibernate关系映射(三) 多对多
一.使用用户User和Role实现多对多的示例 User.java,实现对Role的引用 package com.lxit.entity; import java.util.HashSet; impo ...