c#调用带有安全认证的java webservice

C#调用java带有安全认证的WebService时,自动生成的代理里不包含Header内容,因此需要自己填补这些信息。整理几个调用方法如下:

1. .net framework 3.0以上直接引用服务,通过配置完成,

步骤:在client->endpoint添加headers内容,完整的配置如下:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3   <system.serviceModel>
 4     <bindings>
 5       <basicHttpBinding>
 6         <binding name="PspForBidWSServiceSoapBinding" />
 7       </basicHttpBinding>
 8     </bindings>
 9     <client>
10       <endpoint address="http://127.0.0.1/services/pspforbidws"
11           binding="basicHttpBinding" bindingConfiguration="PspForBidWSServiceSoapBinding"
12           contract="SXXH.PspForBidWS" name="PspForBidWSPort" >
13         <!--添加 headers-->
14         <headers>
15           <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" soap:mustUnderstand="1">
16             <wsse:UsernameToken wsu:Id="UsernameToken-4c3ba901-520b-4e01-931b-7904df54fd9a">
17               <wsse:Username>01011001011</wsse:Username>
18               <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">Bqm1y5JtKWYjEk92exBF3gDIXMk=</wsse:Password>
19               <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">Rb+Kmx7s+FapANdzF1r5wg==</wsse:Nonce>
20               <wsu:Created>2016-04-28T06:36:52.739Z</wsu:Created>
21             </wsse:UsernameToken>
22           </wsse:Security>
23         </headers>
24         <!--结束-->
25       </endpoint>
26     </client>
27   </system.serviceModel>
28 </configuration>
View Code

 

2. 拼装soap使用http post ,将验证信息放入Header中,代码如下:

  1 using System;
  2 using System.IO;
  3 using System.Net;
  4 using System.Text;
  5 using System.Web;
  6 
  7 namespace WebServiceClient
  8 {
  9     public class ServiceSoap
 10     {
 11 
 12         public static string GJSendSOAPMessage(string url, string soapXml)
 13         {
 14             string responseMsg = String.Empty;
 15             HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
 16             req.Method = "POST";
 17             req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
 18             Encoding myEncoding = Encoding.GetEncoding("utf-8");
 19             string param = HttpUtility.UrlEncode("xml", myEncoding) + "=" + HttpUtility.UrlEncode(soapXml, myEncoding);
 20             byte[] postBytes = Encoding.UTF8.GetBytes(param);
 21             req.ContentLength = postBytes.Length;
 22             try
 23             {
 24                 using (Stream reqStream = req.GetRequestStream())
 25                 {
 26                     reqStream.Write(postBytes, 0, postBytes.Length);
 27                     reqStream.Close();
 28                     reqStream.Dispose();
 29                 }
 30                 using (WebResponse wr = req.GetResponse())//在这里对接收到的页面内容进行处理
 31                 {
 32                     System.IO.Stream res = wr.GetResponseStream();
 33                     System.IO.StreamReader reader = new System.IO.StreamReader(res);
 34                     responseMsg = reader.ReadToEnd(); //返回结果
 35                     wr.Close();
 36                 }
 37             }
 38             catch (Exception ex)
 39             {
 40                 throw ex;
 41             }
 42             return responseMsg;
 43         }
 44 
 45         public static void SendSoap()
 46         {
 47             try
 48             {
 49                 string url = "http://127.0.0.1:7090/services/test";//?wsdl";
 50                 string datastr = GetSoap();
 51                 //request
 52                 //Uri uri = new Uri(url);
 53                 //WebRequest webRequest = WebRequest.Create(uri);
 54                 //webRequest.ContentType = "text/xml; charset=utf-8";
 55                 //webRequest.Method = "POST";
 56                 //using (Stream requestStream = webRequest.GetRequestStream())
 57                 //{
 58                 //    byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
 59                 //    requestStream.Write(paramBytes, 0, paramBytes.Length);
 60                 //}
 61                 ////response
 62                 //WebResponse webResponse = webRequest.GetResponse();
 63                 //using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
 64                 //{
 65                 //    string result = "";
 66                 //    return result = myStreamReader.ReadToEnd();
 67                 //}
 68                 string res = GJSendSOAPMessage(url, datastr);
 69                 Console.WriteLine(res);
 70             }
 71             catch (Exception ex)
 72             {
 73                 throw ex;
 74             }
 75 
 76         }
 77 
 78         public static string GetSoap()
 79         {
 80             StringBuilder soap = new StringBuilder();
 81             soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
 82             soap.Append("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
 83 
 84             #region Header
 85             soap.Append("<SOAP-ENV:Header xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
 86             soap.Append("<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" soap:mustUnderstand=\"1\">");
 87             soap.Append("<wsse:UsernameToken wsu:Id=\"UsernameToken - 4c3ba901 - 520b - 4e01 - 931b - 7904df54fd9a\">");
 88             soap.Append("<wsse:Username>用户名</wsse:Username>");
 89             soap.Append("<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest\">密码</wsse:Password>");
 90             soap.Append("<wsse:Nonce EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">Rb+Kmx7s+FapANdzF1r5wg==</wsse:Nonce>");
 91             soap.Append("<wsu:Created>2016-04-28T06:36:52.739Z</wsu:Created>");
 92             soap.Append("</wsse:UsernameToken>");
 93             soap.Append("</wsse:Security>");
 94             soap.Append("</SOAP-ENV:Header>");
 95             #endregion Header end
 96 
 97             #region Body
 98 
 99             soap.Append("<soap:Body>");
100             soap.Append("<ns2:transferBidNotice xmlns:ns2=\"http://ws.webservice.psp.sxca.com/\">");
101             soap.Append("<bidNoticeWS>");
102             soap.Append("<accordToLaw>1</accordToLaw>");
103             soap.Append("<agencyOrg>测试代理机构名称</agencyOrg>");
104             soap.Append("<approveCode>A001</approveCode>");
105             soap.Append("<area>140100</area>");
106             soap.Append("<bidOpenTime>2015-12-12T00:00:00.000+08:00</bidOpenTime>");
107             soap.Append("<bidProjectCode>A001</bidProjectCode>");
108             soap.Append("<bidProjectName>测试招标项目</bidProjectName>");
109             soap.Append("<bidSchemeApproveCode>A001</bidSchemeApproveCode>");
110             soap.Append("<businessId>fca99cef7eb04ebe8b0f84b341b6a3b6</businessId>");
111             soap.Append("<businessLicence>140500200014001</businessLicence>");
112             soap.Append("<funding>中央政府资金</funding>");
113             soap.Append("<industryClassification>A01</industryClassification>");
114             soap.Append("<investment>123.0</investment>");
115             soap.Append("<moreAnnexList>");
116             soap.Append("<annexContent>suLK1Li9vP7E2sjdMQ==</annexContent>");
117             soap.Append("<annexName>测试附件名称1</annexName>");
118             soap.Append("</moreAnnexList>");
119             soap.Append("<moreAnnexList>");
120             soap.Append("<annexContent>suLK1Li9vP7E2sjdMg==</annexContent>");
121             soap.Append("<annexName>测试附件名称2</annexName>");
122             soap.Append("</moreAnnexList>");
123             soap.Append("<natureId>1</natureId>");
124             soap.Append("<natureTypeId>1</natureTypeId>");
125             soap.Append("<noticeContent>测试公告内容</noticeContent>");
126             soap.Append("<noticeTitle>测试公告标题</noticeTitle>");
127             soap.Append("<platformBidProjectCode>14000100001</platformBidProjectCode>");
128             soap.Append("<projectName>测试项目</projectName>");
129             soap.Append("<releaseType>C</releaseType>");
130             soap.Append("<resourceType>1</resourceType>");
131             soap.Append("<tendereeOrg>测试业主名称</tendereeOrg>");
132             soap.Append("</bidNoticeWS>");
133             soap.Append("</ns2:transferBidNotice>");
134             soap.Append("</soap:Body>");
135             soap.Append("</soap:Envelope>");
136 
137             #endregion Body End
138 
139             return soap.ToString();
140         }
141 
142     }
143 }
View Code

 

posted @ 2016-05-05 14:43  J.Y  阅读(558)  评论(0编辑  收藏  举报