Inserting items into the SOAP Header from C# Client to call a webservice

J

John

I'm trying to call a Webservice (Non-.NET) That requires the insertion
of security credentials into the SOAP header. Up until know I've been
creating Dynamic proxy classes to call web services and not been
dealing with the inner workings of SOAP.

Looks Like I need to learn a little about soap and Manually calling
Web Services.....

Any help will be very appreciated !!!

I've been told my SOAP message needs to look as follows:

<SOAP-ENV:Header>
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/
secext">
<wsse:UsernameToken>
<wsse:Username>AUSER</wsse:Username>
<wsse:password Type="wsse:passwordText">APassword</
wsse:password>
</wsse:UsernameToken>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:Search_spcUser xmlns:ns1="http://myapp.com/asi">
<UserId>John</UserId>
</ns1:Search_spcUser>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
 
J

John Bailo

Well, if it were a c# webservice, it would have a class such as:

public class AuthHeader : SoapHeader
{
public string Username;
public string Password;
}

And the method would have the [SoapHeader ("Authentication")] attribute.


From your client you would consume the webservice like:

private Wsvs.AuthHeader Authorization = new Wsvs.AuthHeader();

and

//SOAP Authorization request
Authorization.Username="test";
Authorization.Password="test";
reg.AuthHeaderValue=Authorization;


Can you not do something like this with the Non-.NET webservice?
 
J

John

Unfortunately I have no control over the "Non-.NET webservice" so I
have to figure out a way to manually insert into the SOAP Header. I've
never Manually created a SOAP request and passed it to a Web
Service. I'm hoping it is *fairly* straightforward.
 
A

Ashot Geodakov

Hi,

Try this please:


static void Main( string[] args )
{
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(
"http://url_of_your_web_service" );
string strSOAPRequestBody = "<SOAP-ENV:Header>" +
"<wsse:Security
xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/07/secext\">" +
"<wsse:UsernameToken>" +
"<wsse:Username>AUSER</wsse:Username>" +
"<wsse:password
Type=\"wsse:passwordText\">APassword</wsse:password>" +
"</wsse:UsernameToken>" +
"</wsse:Security>" +
"</SOAP-ENV:Header>" +
"<SOAP-ENV:Body>" +
"<ns1:Search_spcUser xmlns:ns1=\"http://myapp.com/asi\">" +
"<UserId>John</UserId>" +
"</ns1:Search_spcUser>" +
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
request.Method = "POST";
request.ContentType = "application/soap+xml; charset=utf-8";
request.ContentLength = strSOAPRequestBody.Length;
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter( request.GetRequestStream() );
streamWriter.Write( strSOAPRequestBody );
streamWriter.Close();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(
request.GetResponse().GetResponseStream() );
string strResponse = "";
while( !streamReader.EndOfStream )
strResponse += streamReader.ReadLine();
reader.Close();
}


At the end, strResponse will contain the XML of whatever the service has
returned to you. Of course you will need to parse that XML into a
XmlDocument and use that document to read whatever data is there.
 
J

John Bailo

John said:
Unfortunately I have no control over the "Non-.NET webservice" so I
have to figure out a way to manually insert into the SOAP Header. I've
never Manually created a SOAP request and passed it to a Web
Service. I'm hoping it is *fairly* straightforward.

I've done such in Java.

I open a socket and send the raw SOAP/XML header and data to consume the
web service.

It wasn't a big deal, but I wasn't using an authorized web service
either. There's just a lot of string manipulation and parsing involved.


So it looks like:

// send an HTTP request to the web service
out.println("POST " + WebServicePath + " HTTP/1.1");
out.println("Host: " + Server);
out.println("Content-Type: text/xml; charset=utf-8");
out.println("Content-Length: " + String.valueOf(length));
//out.println("Content-Length: " + "398");
out.println("SOAPAction: \"" + SoapAction + "\"");
out.println("Connection: Close");
out.println();

out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
out.println("<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/\">");
out.println("<soap:Body>");
out.println("<" + MethodName + " xmlns=\"" + XmlNamespace + "\">");

//Parameters passed to the method are added here
for (int t = 0; t < namevec.size(); t++) {
String name = (String) namevec.elementAt(t);
String data = (String) datavec.elementAt(t);
out.println("<" + name + ">" + data + "</" + name + ">");

}

out.println("</" + MethodName + ">");
out.println("</soap:Body>");
out.println("</soap:Envelope>");
out.println();
 
J

John Bailo

John said:
Unfortunately I have no control over the "Non-.NET webservice" so I
have to figure out a way to manually insert into the SOAP Header. I've
never Manually created a SOAP request and passed it to a Web
Service. I'm hoping it is *fairly* straightforward.


BTW,

You should still be able to add a WebReference to your client project
and see what's available as far as methods on the Non-.NET webservice by
using the object browser.
 
M

Mitko

I had the same issue and Ashot's code worked perfectly for me! Thank
you for the great example!

Dimitar



Hi,

Try this please:

static void Main( string[] args )
{
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(
"http://url_of_your_web_service" );
string strSOAPRequestBody = "<SOAP-ENV:Header>" +
"<wsse:Security
xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/07/secext\">" +
"<wsse:UsernameToken>" +
"<wsse:Username>AUSER</wsse:Username>" +
"<wsse:password
Type=\"wsse:passwordText\">APassword</wsse:password>" +
"</wsse:UsernameToken>" +
"</wsse:Security>" +
"</SOAP-ENV:Header>" +
"<SOAP-ENV:Body>" +
"<ns1:Search_spcUser xmlns:ns1=\"http://myapp.com/asi\">" +
"<UserId>John</UserId>" +
"</ns1:Search_spcUser>" +
"</SOAP-ENV:Body>" +
"</SOAP-ENV:Envelope>";
request.Method = "POST";
request.ContentType = "application/soap+xml; charset=utf-8";
request.ContentLength = strSOAPRequestBody.Length;
System.IO.StreamWriter streamWriter =
new System.IO.StreamWriter( request.GetRequestStream() );
streamWriter.Write( strSOAPRequestBody );
streamWriter.Close();
System.IO.StreamReader streamReader =
new System.IO.StreamReader(
request.GetResponse().GetResponseStream() );
string strResponse = "";
while( !streamReader.EndOfStream )
strResponse += streamReader.ReadLine();
reader.Close();
}

At the end, strResponse will contain the XML of whatever theservicehas
returned to you. Of course you will need to parse that XML into a
XmlDocument and use that document to read whatever data is there.




Unfortunately I have no control over the "Non-.NETwebservice" so I
have to figure out a way to manually insert into theSOAPHeader. I've
never Manually created aSOAPrequest and passed it to aWeb
Service. I'm hoping it is *fairly* straightforward.- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top