C # calls a Java WebService with secure authentication

Source: Internet
Author: User
Tags getmessage http post soap wsdl

Recently use C # call another colleague wrote Java WebService delay a lot of time, online information is not too complete, go a lot of detours, hope to everyone helpful.

The basic idea is
1. Assemble soap using HTTP POST, mainly to verify the identity information into the header, the following code for reference: 8-15 in-line users, passwords, other soap information needs to be modified according to their own service,

You can use SOAPUI to get body and xmlns information

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\ ">");
Ten soap. Append ("<wsse:UsernameToken>");
Soap. Append ("<wsse:Username>username</wsse:Username>");//username
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>");//password
Soap. Append ("</wsse:UsernameToken>");
Soap. Append ("</wsse:Security>");
Soap. Append ("</soapenv:Header>");
Soap. Append ("<soapenv:Body>");
Soap. Append ("<end:service1>");
Soap. Append ("<arg0></arg0>");
Soap. Append ("</end:service1>");
Soap. Append ("</soapenv:Body>");
Soap. Append ("</soapenv:Envelope>");
22
String url = "Http://localhost/end:service1";
The var result = Getsoapresource (URL, soap. ToString ());
25
26}
27
public static string Getsoapresource (string url, string datastr)
29 {
Try
31 {
//request
The URI uri = new uri (URL);
WebRequest WebRequest = WebRequest.Create (URI);
Webrequest.contenttype = "Text/xml; Charset=utf-8 ";
Webrequest.method = "POST";
PNS using (Stream requeststream = Webrequest.getrequeststream ())
38 {
byte[] parambytes = Encoding.UTF8.GetBytes (datastr. ToString ());
requestStream.Write (parambytes, 0, parambytes.length);
41}
//response
WebResponse WebResponse = Webrequest.getresponse ();
using (StreamReader Mystreamreader = new StreamReader (WebResponse.GetResponseStream (), Encoding.UTF8))
45 {
string result = "";
return result = Mystreamreader.readtoend ();
48}
49
50}
Exception-catch (ex)
52 {
A-n throw ex;
54}
55
56}
57
58}

2. Using WSDL to generate the proxy class, the proxy class overrides HttpRequest, adds security authentication information to the request, and does not test through

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 user Name
6 NC. Password = "Ssotest";//java WebService password
7 request. Credentials = NC;
8
9 return request;
10}
Here's an easy way to do that:
Directly using the service reference in. NET, note that a service reference (similar to a WCF reference) sets the security authentication information primarily through ServiceModel, which is supported by framework3.0.
After the reference to the config in the ServiceModel endpoint node increased headers, security authentication information to increase as far as possible.
The following is the complete config:1 <system.serviceModel>
2 <bindings>
3 <basicHttpBinding>
4 <binding name= "service1soapbinding" maxreceivedmessagesize= "99999999"/>
5 </basicHttpBinding>
6 </bindings>
7 <client>
8 <endpoint address= "Http://local:9090/Service1"
9 binding= "BasicHttpBinding" bindingconfiguration= "onlineuserservicesoapbinding"
Ten contract= "SMPWCF. Service1 "name=" Service1implport ">
<wsse:security xmlns:wsse= "Http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext -1.0.xsd ">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:password type= "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token- Profile-1.0#passwordtext ">password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</endpoint>
</client> </system.serviceModel>

Additional PHP soap calls Java Wsse WebService code (already tested)

<?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 headers, 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 "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> parameters </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 "echo $e->getmessage ();
}
?>

C # calls a Java WebService with secure authentication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.