国庆长假到了,本想出去玩玩,无奈自己屌丝一枚,啥都没有,只能自己宅在家里思考思考人生。不过人生还是过于复杂,一时间也想不出个所以然,只能是整理一下在工作中遇到的一些小问题,首先是关于带soapheader的webservice。

一、webservice大家都用的比较频繁,有时也有一些带soapheader的webservice,首先一种最简单的调用soapheader的情况就是,如果对方的webservice也是用.net写的,可能会是这种方式

     [WebMethod]
[SoapHeader("Header")]
public string HelloWorld()
{
if (Header.username == "admin" && Header.password == "")
{
return "Hello World";
}
else
{
throw new Exception("验证失败");
}
} public class AuthHeader : SoapHeader
{
public string username;
public string password;
}

之后我们在通过添加服务引用或者是利用vs的wsdl工具生成代理类,都会把上面的AuthHeader类型给生成好,我们要做的就是简单的赋值工作了

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//要调用的webservice的类型,自动生成在代理类中
SoapHeaderTest soapTest = new SoapHeaderTest();
//要调用的soapheader的类型,自动生成在代理类中
AuthHeader authHeader = new AuthHeader();
authHeader.username = "admin";
authHeader.password = "";
soapTest.AuthHeaderValue = authHeader;
string content = soapTest.HelloWorld();
context.Response.Write(content);
}

通过这种方式就可以通过验证调用webservice获取返回信息了。

二、有些时候我们发现我们调用对方的webservice一直失败,然后添加的服务引用或者是代理类中也没有soapheader的类型,然后客户告诉我们,你要调用接口必须传soapHeader,这个soapHeader在.net中是这样的

    [DataContract(Namespace = "http://xxx.xxx.xxxxx")]
public class AuthHeader
{
public string username { get; set; }
public string password { get; set; }
}

我们把这个AuthHeader按照上面的格式写好。然后在调用webservice中的方法之前加上我们的soapheader,代码如下:

        //添加服务引用生成的类型
SoapTestService.SoapHeaderTestSoapClient client = new SoapTestService.SoapHeaderTestSoapClient();
//客户告诉我们AuthHeader的类型,然后自己在程序中对应写出来
AuthHeader header = new AuthHeader();
header.username = "admin";
header.password = "";
//开始加入监控头信息
AddressHeader soapheader = AddressHeader.CreateAddressHeader("AuthHeader", // Header Name
"http:xxx.xxx.xxxxx",//地址头的命名空间
header);//传人的AuthHeader
EndpointAddressBuilder eab = new EndpointAddressBuilder(client.Endpoint.Address);
eab.Headers.Add(soapheader);//将地址头加入进去
client.Endpoint.Address = eab.ToEndpointAddress();
//结束加入监控头信息

之后在调用webservice的方法就可以成功调用并获取返回内容了。

三、最后一种情况就是人家只告诉你需要加一个这样的

<AuthHeader>

<username>用户名</username>

<password>密码</password>

</AuthHeader>

这个时候就需要使用我们的SoapUI了,我们来用soapui看看我们报文吧

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">
<soap:Header/>
<soap:Body>
<tem:HelloWorld/>
</soap:Body>
</soap:Envelope>

发现怎么<soap:Header/>中是空的呢,然后我们按照别人给的格式将soapheader中填上

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">
<soap:Header>
<AuthHeader>
<username>admin</username>
<password>123</password>
</AuthHeader>
</soap:Header>
<soap:Body>
<tem:HelloWorld/>
</soap:Body>
</soap:Envelope>

然后这样发送过去,发现webservice成功访问并且接收到返回值了,哎,任务时间比较紧迫,只能用最简单也是最笨的方法了,替换数据然后在把报文发过去

 string url = ConfigurationManager.AppSettings["url地址"].ToString();

                var webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
string UserName = ConfigurationManager.AppSettings["用户名"].ToString();
string Password = ConfigurationManager.AppSettings["密码"].ToString();
webRequest.PreAuthenticate = true;
NetworkCredential networkCredential = new NetworkCredential(UserName, Password);
webRequest.Credentials = networkCredential;
byte[] bs = Encoding.UTF8.GetBytes(SoapXml);
webRequest.Method = "POST";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
webRequest.ContentLength = bs.Length;
using (Stream reqStream = webRequest.GetRequestStream())
{
reqStream.Write(bs, , bs.Length);
}
//reqStream.Close(); WebResponse myWebResponse = webRequest.GetResponse();
string result;
using (StreamReader sr = new StreamReader(myWebResponse.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
{
// 返回结果
result = sr.ReadToEnd();
}

最终返回的xml中的内容也只能是自己解析了。。。

可能遇到后面两种情况的会少一些,希望大家有其他的方式可以分享一下。最后祝大家国庆节快乐!

.net 在不同情况下调用带soapheader的webservice的方式的更多相关文章

  1. 什么情况下调用doGet()和doPost()?

    默认情况是调用doGet()方法,JSP页面中的Form表单的method属性设置为post的时候,调用的为doPost()方法: 为get的时候,调用deGet()方法.

  2. 某些情况下调用函数为什么要在函数名前加“(void)”

    我们知道,在定义函数时,加在函数名前的"void"表示该函数没有返回值.但在调用时,在函数名前加"(void)"的作用又是什么呢? 最明显的一点就是表示程序并不 ...

  3. 什么情况下调用doGet()和doPost()?

    Jsp页面中的FORM标签里的method属性为get时调用doGet(),为post时调用doPost().

  4. Django在不启动server的情况下调用方法

    from django.conf import settingsfrom django import template settings.configure() a = template.Templa ...

  5. layoutSubviews在什么情况下调用

    可以使用layoutSubviews修改UI: 1.init初始化不会触发layoutSubviews 但是是用initWithFrame 进行初始化时,当rect的值不为CGRectZero时,也会 ...

  6. C#/PHP调用有SoapHeader的WebService

    日前调用第三方WebService接口时遇到了SoapHeader验证的问题,记录一下解决方法. 接口需要的格式: <soapenv:Header> <ReqSOAPHeader x ...

  7. Ajax在调用含有SoapHeader的webservice方法

    ·         [WebService(Namespace = "http://tempuri.org/")] ·             [WebServiceBinding ...

  8. asp.net 调用带证书的webservice解决办法

    最近在朋友弄一个调整省政府政务工作流的程序.. 需要把当前的信息推送到政务网上,采用的是带证书的https webservice.. 下面说一下实现过程 第一步,引用webservice地址,删除we ...

  9. x86 x64下调用约定浅析

    x86平台下调用约定 我们都知道x86平台下常用的有三种调用约定,__cdecl.__stdcall.__fastcall.我们分别对这三种调用约定进行分析. __cdecl __cdecl是C/C+ ...

随机推荐

  1. Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)

    题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...

  2. Codevs No.1553 互斥的数

    2016-05-31 21:34:15 题目链接: 互斥的数 (Codevs No.1553) 题目大意: 给N个数,如果其中两个数满足一个数是另一个的P倍,则称它俩互斥,求一个不互斥集合的最大容量 ...

  3. leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)

    https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and ...

  4. Xcode 的正确打开方式——Debugging

    程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode.这篇博客就主要介绍了 Xcode 中几种能够大幅提升代码调试效率的方式. “If debuggi ...

  5. ocp 1Z0-043 1-60题解析

    1.You observe that a database performance has degraded overa period of time. While investigating the ...

  6. POJ 2387 Til the Cows Come Home (最短路 dijkstra)

    Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...

  7. iOS学习之基本概念

    学习iOS最重要的是态度和兴趣,如果你对于学习始终抱有不断的热情和端正的态度,那么,无论是什么,你总会成功的! 有一句话与大家共勉:过程中跌倒多少次都没有关系,重要的是,跌倒后你能够站起来重新寻找正确 ...

  8. [iOS 多线程 & 网络 - 2.11] - ASI框架上传文件

    A.ASI的上传功能基本使用 1.实现步骤 (1)创建请求 使用ASIFormDataRequest (2)设置上传文件路径 (3)发送请求     2.上传相册相片 UIImagePickerCon ...

  9. Quality Center 使用IE8异常浏览器打开

    需要装2个软件 1.  Microsoft Visual C++ 2005 Redistributable Package (x64) 2.  dotnetfx35.exe 配置IE的选项 使用兼容模 ...

  10. 【转】2013年中国IT业10大公司

    转自:http://www.chinaz.com/news/2013/1217/331446.shtml?zyy 1.最得志的公司:小米 在2013年,再没有一家公司像小米这样志得意满,即便看看所有的 ...