469,327 Members | 1,265 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

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

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>

Feb 26 '07 #1
6 46263

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?
John wrote:
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>
Feb 26 '07 #2
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.
Feb 26 '07 #3
Hi,

Try this please:
static void Main( string[] args )
{
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.C reate(
"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.
"John" <19*****@gmail.comwrote in message
news:11**********************@k78g2000cwa.googlegr oups.com...
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.


Feb 26 '07 #4
John wrote:
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();
Feb 27 '07 #5
John wrote:
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.

Feb 27 '07 #6

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

Dimitar

On Feb 26, 6:06 pm, "Ashot Geodakov" <a_geoda...@hotmail.comwrote:
Hi,

Try this please:

static void Main( string[] args )
{
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.C reate(
"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.

"John" <1944...@gmail.comwrote in message

news:11**********************@k78g2000cwa.googlegr oups.com...
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 -

Apr 20 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Q. John Chen | last post: by
6 posts views Thread by john deviney | last post: by
reply views Thread by Daniel Thune, MCSE | last post: by
6 posts views Thread by A.M-SG | last post: by
reply views Thread by Samuel Zallocco | last post: by
6 posts views Thread by Peter van der veen | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.