最近使用c#調用另外一個同事寫的java webservice耽誤了很多時間,網上資料不太完整,走了很多彎路,希望對大家有幫助。
基本思路是
1.拼裝soap使用http post ,主要將驗證身份信息放入header中,以下code供參考:8-15行內用戶、密碼,其他soap信息需要根據自己的service修改,
1.拼裝soap使用http post ,主要將驗證身份信息放入header中,以下code供參考:8-15行內用戶、密碼,其他soap信息需要根據自己的service修改,
可以使用soapui獲取到body以及xmlns信息
1
public
class InvokeServiceWithSoap
2 {
3 public static void InvokeService()
4 {
5 StringBuilder soap = new StringBuilder();
6 soap.Append( " <?xml version=\"1.0\" encoding=\"utf-8\"?> ");
7 soap.Append( " <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:end=\"http://localhost/service/\"> ");
8 soap.Append( " <soapenv:Header> ");
9 soap.Append( " <wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"> ");
10 soap.Append( " <wsse:UsernameToken> ");
11 soap.Append( " <wsse:Username>username</wsse:Username> "); // 用戶名
12 soap.Append( " <wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">password</wsse:Password> "); // 口令
13 soap.Append( " </wsse:UsernameToken> ");
14 soap.Append( " </wsse:Security> ");
15 soap.Append( " </soapenv:Header> ");
16 soap.Append( " <soapenv:Body> ");
17 soap.Append( " <end:service1> ");
18 soap.Append( " <arg0></arg0> ");
19 soap.Append( " </end:service1> ");
20 soap.Append( " </soapenv:Body> ");
21 soap.Append( " </soapenv:Envelope> ");
22
23 string url = " http://localhost/end:service1 ";
24 var result = GetSOAPReSource(url, soap.ToString());
25
26 }
27
28 public static string GetSOAPReSource( string url, string datastr)
29 {
30 try
31 {
32 // request
33 Uri uri = new Uri(url);
34 WebRequest webRequest = WebRequest.Create(uri);
35 webRequest.ContentType = " text/xml; charset=utf-8 ";
36 webRequest.Method = " POST ";
37 using (Stream requestStream = webRequest.GetRequestStream())
38 {
39 byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
40 requestStream.Write(paramBytes, 0, paramBytes.Length);
41 }
42 // response
43 WebResponse webResponse = webRequest.GetResponse();
44 using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
45 {
46 string result = "";
47 return result = myStreamReader.ReadToEnd();
48 }
49
50 }
51 catch (Exception ex)
52 {
53 throw ex;
54 }
55
56 }
57
58 }
2 {
3 public static void InvokeService()
4 {
5 StringBuilder soap = new StringBuilder();
6 soap.Append( " <?xml version=\"1.0\" encoding=\"utf-8\"?> ");
7 soap.Append( " <soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:end=\"http://localhost/service/\"> ");
8 soap.Append( " <soapenv:Header> ");
9 soap.Append( " <wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"> ");
10 soap.Append( " <wsse:UsernameToken> ");
11 soap.Append( " <wsse:Username>username</wsse:Username> "); // 用戶名
12 soap.Append( " <wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">password</wsse:Password> "); // 口令
13 soap.Append( " </wsse:UsernameToken> ");
14 soap.Append( " </wsse:Security> ");
15 soap.Append( " </soapenv:Header> ");
16 soap.Append( " <soapenv:Body> ");
17 soap.Append( " <end:service1> ");
18 soap.Append( " <arg0></arg0> ");
19 soap.Append( " </end:service1> ");
20 soap.Append( " </soapenv:Body> ");
21 soap.Append( " </soapenv:Envelope> ");
22
23 string url = " http://localhost/end:service1 ";
24 var result = GetSOAPReSource(url, soap.ToString());
25
26 }
27
28 public static string GetSOAPReSource( string url, string datastr)
29 {
30 try
31 {
32 // request
33 Uri uri = new Uri(url);
34 WebRequest webRequest = WebRequest.Create(uri);
35 webRequest.ContentType = " text/xml; charset=utf-8 ";
36 webRequest.Method = " POST ";
37 using (Stream requestStream = webRequest.GetRequestStream())
38 {
39 byte[] paramBytes = Encoding.UTF8.GetBytes(datastr.ToString());
40 requestStream.Write(paramBytes, 0, paramBytes.Length);
41 }
42 // response
43 WebResponse webResponse = webRequest.GetResponse();
44 using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
45 {
46 string result = "";
47 return result = myStreamReader.ReadToEnd();
48 }
49
50 }
51 catch (Exception ex)
52 {
53 throw ex;
54 }
55
56 }
57
58 }
2.使用wsdl生成代理類,代理類重寫HttpRequest ,將安全驗證信息增加到request中,也沒有測試通過
1
protected
override System.Net.WebRequest GetWebRequest(Uri uri)
2 {
3 System.Net.WebRequest request= base.GetWebRequest(uri);
4 System.Net.NetworkCredential nc = new System.Net.NetworkCredential();
5 nc.UserName = " ssotest "; // java webservice 用戶名
6 nc.Password = " ssotest "; // java webservice 密碼
7 request.Credentials = nc;
8
9 return request;
10 }
2 {
3 System.Net.WebRequest request= base.GetWebRequest(uri);
4 System.Net.NetworkCredential nc = new System.Net.NetworkCredential();
5 nc.UserName = " ssotest "; // java webservice 用戶名
6 nc.Password = " ssotest "; // java webservice 密碼
7 request.Credentials = nc;
8
9 return request;
10 }
下面說一個比較簡單的方法:
直接使用.net中的服務引用,注意是服務引用(類似WCF引用)主要通過servicemodel來設置安全認證信息,framework3.0以上都支持,
引用后將 Config中 servicemodel 的 endpoint 節點 增加headers ,安全驗證信息增加盡量來即可。
以下是完整的config:
1 <system.serviceModel>
2 <bindings>
3 <basicHttpBinding>
4 <binding name= " Service1 SoapBinding " maxReceivedMessageSize= " 99999999 "/>
5 </basicHttpBinding>
6 </bindings>
7 <client>
8 <endpoint address= " http://local:9090/ Service1 "
9 binding= " basicHttpBinding " bindingConfiguration= " onlineUserServiceSoapBinding "
10 contract= " smpwcf.Service1 " name= " Service1 ImplPort " >
11 <headers>
12 <wsse:Security xmlns:wsse= " http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd ">
13 <wsse:UsernameToken>
14 <wsse:Username>username</wsse:Username>
15 <wsse:Password Type= " http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText ">password</wsse:Password>
16 </wsse:UsernameToken>
17 </wsse:Security>
18 </headers>
19 </endpoint>
20 </client> 21 </system.serviceModel>
2 <bindings>
3 <basicHttpBinding>
4 <binding name= " Service1 SoapBinding " maxReceivedMessageSize= " 99999999 "/>
5 </basicHttpBinding>
6 </bindings>
7 <client>
8 <endpoint address= " http://local:9090/ Service1 "
9 binding= " basicHttpBinding " bindingConfiguration= " onlineUserServiceSoapBinding "
10 contract= " smpwcf.Service1 " name= " Service1 ImplPort " >
11 <headers>
12 <wsse:Security xmlns:wsse= " http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd ">
13 <wsse:UsernameToken>
14 <wsse:Username>username</wsse:Username>
15 <wsse:Password Type= " http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText ">password</wsse:Password>
16 </wsse:UsernameToken>
17 </wsse:Security>
18 </headers>
19 </endpoint>
20 </client> 21 </system.serviceModel>
另外附加一段php soap調用java wsse webservice代碼 (已經測試通過)
<?php
//
Soap Request
class WSSESoapClient extends SoapClient {
protected $wsseUser;
protected $wssePassword;
public function setWSSECredentials( $user, $password) {
$this->wsseUser = $user;
$this->wssePassword = $password;
}
public function __doRequest( $request, $location, $action, $version)
{
try
{
if (! $this->wsseUser or ! $this->wssePassword)
{
return parent::__doRequest( $request, $location, $action, $version);
}
// get SOAP message into DOM
$dom = new DOMDocument();
$dom->loadXML( $request);
$xp = new DOMXPath( $dom);
$xp->registerNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
// search for SOAP header, create one if not found
$header = $xp->query('/soapenv:Envelope/soapenv:Header')->item(0);
if (! $header)
{
$header = $dom->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'soapenv:Header');
$envelope = $xp->query('/soapenv:Envelope')->item(0);
$envelope->insertBefore( $header, $xp->query('/soapenv:Envelope/soapenv:Body')->item(0));
}
// add WSSE header
$security = $dom->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse:Security');
$usernameToken = $dom->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse:UsernameToken');
$username = $dom->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse:Username', $this->wsseUser);
$password = $dom->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse:Password', $this->wssePassword);
$password->setAttribute( "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText" );
$security->appendChild( $usernameToken);
$usernameToken->appendChild( $username);
$usernameToken->appendChild( $password);
$header->appendChild( $security);
// perform SOAP call
$request = $dom->saveXML();
// echo $request;
return parent::__doRequest( $request, $location, $action, $version);
}
catch ( Exception $e)
{
echo "<h2>Post Method Error!</h2>";
echo $e->getMessage();
}
}
}
// build soap
try{
$clientsoap = new WSSESoapClient("http://localhost/service1?wsdl");
$clientsoap->setWSSECredentials("username","password");
// $clientsoap->soap_defencoding = 'utf-8';
// $clientsoap->decode_utf8 = false;
//soap xml
$request="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:end=\"http://localhost/service1/\">"."<soapenv:Body>"."<end:service1>"."<arg0>參數</arg0>"."</end:service1>"."</soapenv:Body>"."</soapenv:Envelope>";
$location="http://172.16.2.31:9090/smp/ws/onlineuserservice";
$action= null;
$version=0;
$result = $clientsoap->__doRequest( $request, $location, $action, $version);
echo $result;
}
catch ( Exception $e) {
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}
?>
class WSSESoapClient extends SoapClient {
protected $wsseUser;
protected $wssePassword;
public function setWSSECredentials( $user, $password) {
$this->wsseUser = $user;
$this->wssePassword = $password;
}
public function __doRequest( $request, $location, $action, $version)
{
try
{
if (! $this->wsseUser or ! $this->wssePassword)
{
return parent::__doRequest( $request, $location, $action, $version);
}
// get SOAP message into DOM
$dom = new DOMDocument();
$dom->loadXML( $request);
$xp = new DOMXPath( $dom);
$xp->registerNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
// search for SOAP header, create one if not found
$header = $xp->query('/soapenv:Envelope/soapenv:Header')->item(0);
if (! $header)
{
$header = $dom->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'soapenv:Header');
$envelope = $xp->query('/soapenv:Envelope')->item(0);
$envelope->insertBefore( $header, $xp->query('/soapenv:Envelope/soapenv:Body')->item(0));
}
// add WSSE header
$security = $dom->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse:Security');
$usernameToken = $dom->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse:UsernameToken');
$username = $dom->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse:Username', $this->wsseUser);
$password = $dom->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse:Password', $this->wssePassword);
$password->setAttribute( "Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText" );
$security->appendChild( $usernameToken);
$usernameToken->appendChild( $username);
$usernameToken->appendChild( $password);
$header->appendChild( $security);
// perform SOAP call
$request = $dom->saveXML();
// echo $request;
return parent::__doRequest( $request, $location, $action, $version);
}
catch ( Exception $e)
{
echo "<h2>Post Method Error!</h2>";
echo $e->getMessage();
}
}
}
// build soap
try{
$clientsoap = new WSSESoapClient("http://localhost/service1?wsdl");
$clientsoap->setWSSECredentials("username","password");
// $clientsoap->soap_defencoding = 'utf-8';
// $clientsoap->decode_utf8 = false;
//soap xml
$request="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:end=\"http://localhost/service1/\">"."<soapenv:Body>"."<end:service1>"."<arg0>參數</arg0>"."</end:service1>"."</soapenv:Body>"."</soapenv:Envelope>";
$location="http://172.16.2.31:9090/smp/ws/onlineuserservice";
$action= null;
$version=0;
$result = $clientsoap->__doRequest( $request, $location, $action, $version);
echo $result;
}
catch ( Exception $e) {
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}
?>
http://php.net/manual/en/soapclient.soapclient.php