1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| // Create a UsernameToken security token.
UsernameToken userToken;
userToken = new UsernameToken(username, passwordEquivalent, PasswordOption.SendHashed);
try
{
// Create a new instance of the proxy class.
AddNumbersWse serviceProxy = new AddNumbersWse();
// Get the SoapContext associated with the SOAP request.
SoapContext requestContext = serviceProxy.RequestSoapContext;
// Set the period of time in which the SOAP request expires to one minute.
requestContext.Security.Timestamp.TtlInSeconds = 60;
// Add the UsernameToken token to the SOAP message.
requestContext.Security.Tokens.Add(userToken);
// Sign the SOAP message using the UsernameToken.
requestContext.Security.Elements.Add(new MessageSignature(userToken));
// Call the Web service method.
int sum = serviceProxy.AddInt(a, b);
// If code execution reaches here, the Web service method was
// successfully communicated with.
string message = string.Format("{0} + {1} = {2}", a, b, sum);
MessageBox.Show(message, "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.Web.Services.Protocols.SoapException se)
{
MessageBox.Show(se.ToString());
}
catch (Exception ex)
{
MessageBox.Show ("Exception caught while invoking a web service.", ex.Message );
return;
} |
Partager